main.go:4:2: package lib1 is not in GOROOT (/usr/local/go/src/lib1)
├── 5-init
│ ├── lib1
│ │ └── lib1.go
│ └── lib2
│ └── lib2.go
├── const.go
├── firstVar.go
├── go.mod
├── helloGolang.go
├── main.go
└── test3function.go
go env
发现已经打开了go module GO111MODULE="on"
建立go.mod 文件,引入项目名为module
module firstGolang
go 1.14
import 本地lib时,加上完整的module 路径,解决了问题
main.go:4:2: package 5-init/lib1 is not in GOROOT (/usr/local/go/src/5-init/lib1)
package main
import (
"firstGolang/5-init/lib1"
"firstGolang/5-init/lib2"
)
func main() {
lib1.Test1()
lib2.Test2()
}
package lib1
import "fmt"
func Test1() {
fmt.Println("test lib1 function")
}
func init() {
fmt.Println("init lib1....")
}
package lib2
import "fmt"
func Test2() {
fmt.Println("test lib2 function")
}
func init() {
fmt.Println("init lib2....")
}
总结 init 和 import package顺序
init lib1....
init lib2....
test lib1 function
test lib2 function