Go1.11 发布了Module特性,可以不依赖GOPATH开发项目。
其实对平时开发到影响不大,就是部署依赖管理比较麻烦。

在GOPATH外新建一个项目go-module,然后用go mod初始化一个module

1
go mod init github.com/zhiruchen/go-module

然后在main.go编写测试代码

1
2
3
4
5
6
7
8
9
10
11
12
package main

import (
"fmt"

"github.com/zhiruchen/go-module/common"
)

func main() {
fmt.Println("go module test")
common.Log()
}

用go build ./…下载依赖

1
2
3
4
5
6
7
go build ./...
go: finding go.uber.org/zap v1.9.1
go: downloading go.uber.org/zap v1.9.1
go: finding go.uber.org/atomic v1.3.2
go: finding go.uber.org/multierr v1.1.0
go: downloading go.uber.org/atomic v1.3.2
go: downloading go.uber.org/multierr v1.1.0

运行

1
2
3
go run main.go
go module test
{"level":"info","ts":1535637509.09551,"caller":"common/log.go:12","msg":"failed to fetch URL","url":"https://google.com","attempt":3,"backoff":1}

参考

代码github地址

https://github.com/zhiruchen/go-module