Go,如何导入其他包的结构和字段?



我有下一个问题,如果我试图导出其他包的结构,调用get日期的方法,并使用(struct.field)获取字段,它不工作

//main/other
package other
type Birthday struct{
Day string
}
func (b *Birthday) SetDay(){
b.Day = "10"
}
//main
package main
import ("main/other")
func main(){
f := other.Birthday{}
f.SetDay()
fmt.Println(f.Day) // ""   no return nothing
}

但是当我在结构体的同一文件中使用函数main时,这个就可以工作了。

我刚刚在操场上测试了你的程序:

package main
import (
"fmt"
"play.ground/foo"
)
func main() {
f := foo.Birthday{}
f.SetDay()
fmt.Println(f.Day)
}
-- go.mod --
module play.ground
-- foo/foo.go --
package foo
type Birthday struct {
Day string
}
func (b *Birthday) SetDay() {
b.Day = "10"
}

它工作得很好。确保首先是go mod init yourProject;如"教程:创建Go模块"中所述。