GO111MODULE=on/off
GO111MODULE
GO111MODULEgo-cmpGOPATH
GOPATH
$GOPATH
bin/ # Directory where executable binaries are stored
src/ # Directory where Go source files of the dependent packages are stored
github.com/google/go-cmp/
...
pkg/ # Directory where compiled Go object files are stored
...
其中:
binsrcpkg
go get xxx$GOPATH/srcGO111MODULE=off$GOPATHgo-cmp
$ docker run -it golang:1.13 /bin/bash
$ GO111MODULE=off go get github.com/google/go-cmp/cmp
$ tree -d -L 5 $GOPATH # assume tree is installed
/go
|-- bin
|-- pkg/linux_amd64/github.com/google/go-cmp
`-- src/github.com/google/go-cmp/cmp
tree
根据 Go 1.11 发版时的声明:
This release adds preliminary support for a new concept called “modules,” an alternative to GOPATH with integrated support for versioning and package distribution.
—
modulego module init xxx$GOPATH
$ GO111MODULE=on go get github.com/google/go-cmp/cmp
$ tree -d -L 5 $GOPATH
/go
`-- pkg
|-- mod
| |-- cache
| | `-- download
| | |-- github.com
| | |-- golang.org
| | `-- sumdb
| `-- github.com
| `-- google
| `-- go-cmp@v0.4.0
`-- sumdb
`-- sum.golang.org
module
Module
$GOPATH$GOPATH$GOPATH
$GOPATHtestproject
main.go:
// main.go
package main
func main() {
TestFunc()
}
test_func.go:
// test_func.go
package main
import "k8s.io/klog"
func TestFunc() {
klog.Infoln("Hello Go Modules!")
}
尝试编译并运行,发现能正常跑起来:
/anywhere/outside/gopath/testproject $ GO111MODULE=off go get k8s.io/klog
/anywhere/outside/gopath/testproject $ GO111MODULE=off go run .
I0404 14:58:55.589292 1445 test_func.go:6] Hello Go Modules!
main$GOPATH
将打印信息的方法移到单独模块中,目录结构变成了:
$ tree /anywhere/outside/gopath/testproject
/anywhere/outside/gopath/testproject
|-- main.go
`-- test
`-- func.go
test/func.go:
// test/func.go
package test
import "k8s.io/klog"
func TestFunc() {
klog.Infoln("Hello Go Modules!")
}
同时更新 main.go 中的调用:
// main.go
package main
import "test"
func main() {
XXX.TestFunc()
}
尝试运行会有如下报错:
$ GO111MODULE=off go run .
main.go:3:8: cannot find package "test" in any of:
/usr/local/go/src/test (from $GOROOT)
/go/src/test (from $GOPATH)
root@7d47e56e5fcc:/wy/testproject# vi main.go
main.gotestmoduleGO111MODULE=off$GOPATH
$GOPATHGO111MODULE=off
$GOPATH
$ tree $GOPATH/src/insujang.github.io
/go/src/insujang.github.io
`-- testproject
|-- main.go
`-- test
`-- func.go
main.go
import "insujang.github.io/testproject/test"
再次运行便可以成功了:
$ $GOPATH/src/insujang.github.io/testproject $ GO111MODULE=off go run .
I0404 15:09:40.239612 2034 func.go:6] Hello Go Modules!
Module 的使用
除上回退到旧版依赖管理方式上,还可以使用 module 来解决上面的问题。
首先将项目初始化成 module:
$ /anywhere/outside/gopath/testproject $ go mod init insujang.github.io/testproject
/anywhere/outside/gopath/testproject $ GO111MODULE=on go run .
I0404 15:18:36.553399 2303 func.go:6] Hello Go Modules!
main.go
// main.go
package main
import "insujang.github.io/testproject/test"
func main() {
test.TestFunc()
}
$GOPATH
项目正常运行:
$ /anywhere/outside/gopath/testproject $ GO111MODULE=off go run .
main.go:3:8: cannot find package "insujang.github.io/testproject/test" in any of:
/usr/local/go/src/insujang.github.io/testproject/test (from $GOROOT)
/go/src/insujang.github.io/testproject/test (from $GOPATH)
GO111MODULE 的默认值
GO111MODULEauto
go.mod$GOPATH