golang diff类型的函数提供“未解析的类型”

Im using function like following which works

import (
   "m5/cmd/models"
)

func  TypeCommand(m Modules) string {

…

}

Now I want to change

func (m models.Modules) TypeCommand() string {

}

And now Im getting error “unresolved type modules” , why ? the first function is compiled ...

You cannot add methods to types from outside the package in which they are defined. If you really feel it is necessary then the workaround is to define a new type which embeds the imported type and extend that. You will then have a type which includes all of the original methods as well as the new method:

import (
   "m5/cmd/models"
)

type myModule struct {
    models.Modules
}

func (m myModule) TypeCommand() string {
  // method code here
}

With this example myModule will have all of the exported fields and methods of model.Modules plus the TypeCommand() method.

You can only define method on a type within the same package. You cannot define method on type Modules outside package models.

From Go specification on Method declarations:

The receiver is specified via an extra parameter section preceding the method name. That parameter section must declare a single non-variadic parameter, the receiver. Its type must be of the form T or *T (possibly using parentheses) where T is a type name. The type denoted by T is called the receiver base type; it must not be a pointer or interface type and it must be defined in the same package as the method. The method is said to be bound to the base type and the method name is visible only within selectors for type T or *T.