文件结构如下:
test_code
│ go.mod
│
├─p
│ p_struct.go
│
└─q
q_test.go
q_use.go
各文件内容
// p_struct.go
package p
type Point struct{
X int
Y int
}
//q_use.go
package q
import (
"fmt"
"test_code/p"
)
func q_use() {
p := &p.Point{1,2}
fmt.Printf("x:%v y:%v",p.X,p.Y)
}
//go.mod
//最重要的文件、
module test_code
go 1.14
要在 q_use.go 文件中使用 p_struct.go 中的结构体。
在 go.mod 文件中,将 test_code声明为一个模块,然后在 q_use中就可以导入 test_code 下的 p_struct文件了。
注意了,一个项目中,不同文件夹都可以有go.mod ,就行一个大工程是很多小模块组成的。