英文:
How to use struct that is imported from another package
问题
好的,以下是翻译好的内容:
嗯,我在Player包中有一个名为Player的结构体。
package Player
type Player struct {
name string
speciality string
}
而且我在main包中有一个main函数。
package main
import "pack/Player"
func main() {
var player Player.Player
fmt.Print(player.name)
}
但是在编译后,我得到了以下错误信息:
player.name未定义(无法引用未公开的字段或方法name)
我做错了什么?
英文:
Well, I have my struct Player in package Player
package Player
type Player struct {
name string
speciality string
}
And I have my main function in package main
package main
import "pack/Player"
func main() {
var player Player.Player
fmt.Print(player.name)
}
But after I compile it I get
> player.name undefined (cannot refer to unexported field or method
> name)
What I am doing wrong?
答案1
得分: 30
你需要将结构体的字段导出,以便通过将它们以大写字母开头来访问:
type Player struct {
Name string
Speciality string
}
英文:
You need to export the fields of your structure in order for them to be accessible by having them start with upper case characters:
type Player struct {
Name string
Speciality string
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论