我导入另一个包中定义的结构,当尝试使用它来构造文字时,出现“不是类型”错误。
在publish.go中
type Book struct {
Name string
Author string
Published bool
}
Run Code Online (Sandbox Code Playgroud)
店内.go
import "publish"
func Init() {
var reading publish.Book
b := &reading {
Name: "Learn Go Lang",
Author: "Rob",
Published: true
}
}
Run Code Online (Sandbox Code Playgroud)
错误:读取不是一种类型
在这里,您尝试创建一个“reading”类型的结构
b := &reading {
Name: "Learn Go Lang",
Author: "Rob",
Published: true
}
Run Code Online (Sandbox Code Playgroud)
你想要的是一个类型为publish.Book的结构
b := & publish.Book {
Name: "Learn Go Lang",
Author: "Rob",
Published: true,
}
Run Code Online (Sandbox Code Playgroud)
另外,在多行结构声明的最后一个末尾还需要一个逗号。