如何从Go中的另一个包导入结构
我正在尝试将以下结构导入到Golang中的一个单独的包中如何从Go中的另一个包导入结构,go,struct,import,Go,Struct,Import,我正在尝试将以下结构导入到Golang中的一个单独的包中 package models type Category struct { Title string Description string Parent *Category ParentId int } 进入包控制器,如下所示 import( "website.com/Owner/blog/app/models" ) func (c Category) Update(){ //do somethin
package models
type Category struct {
Title string
Description string
Parent *Category
ParentId int
}
进入包控制器,如下所示
import(
"website.com/Owner/blog/app/models"
)
func (c Category) Update(){
//do something here
}
但是,我得到了错误未解析类型类别
如何使用go将结构导入到单独的包中?您需要完全限定您的名称:不要使用Category,而是使用models.Category。你应该从那里开始。你可以做:
import "fmt"
func main() {
fmt.Println("Hello")
}
或:
或者完全放弃资格,但是,这被认为是糟糕的风格:当查看代码时,能够一目了然地看到某些结构或功能来自何处是非常方便的:
import . "fmt"
func main() {
// Where does that come from ? Hard to know without
// intimate knowledge of the package
Println("Hello")
}
您可以做的最后一件事是“typedef”远程结构,以便于本地使用:
import "website.com/Owner/blog/app/models"
type Category models.Category
但是请注意,它创建了一个,尽管具有相同的基础类型。您需要完全限定您的名称:不要使用Category,而是使用models.Category。你应该从那里开始。你可以做:
import "fmt"
func main() {
fmt.Println("Hello")
}
或:
或者完全放弃资格,但是,这被认为是糟糕的风格:当查看代码时,能够一目了然地看到某些结构或功能来自何处是非常方便的:
import . "fmt"
func main() {
// Where does that come from ? Hard to know without
// intimate knowledge of the package
Println("Hello")
}
您可以做的最后一件事是“typedef”远程结构,以便于本地使用:
import "website.com/Owner/blog/app/models"
type Category models.Category
但是请注意,它创建了一个,尽管具有相同的基础类型。func c models.Category Update{//do somethine here}func c models.Category Update{//do somethine here}