如何解决子文件夹调用文件夹Golang
我的项目名称是家庭。我想使用在father.go 中定义的结构体,在son.go 中使用它。我必须做什么?
解决方法
go.mod
:
module family
father\father.go
:
package father
type Dad struct { Age int }
father\son\son.go
:
package main
import (
"family/father"
"fmt"
)
func main() {
d := father.Dad{40}
fmt.Println(d)
}
,
- father/father.go
package father
type Father struct {
Name string `json:"name"`
Job string `json:"job"`
}
2.father/son/son.go
package son
import (
"fmt"
"github.com/yaocanwei/demo/father"
)
type Son struct {
father.Father
Hobby string `json:"hobby"`
}
func (son *Son) EchoJob() string {
return fmt.Sprintf("%s",son.Father.Job)
}
3.main.go
package main
import (
"fmt"
"github.com/yaocanwei/demo/father/son"
)
func main() {
s := &son.Son{}
s.Job = "senior engineer"
fmt.Println(s.EchoJob())
}