在Go1.11.1中使用go module管理依赖

今天试验了一下go的版本管理Go moule,只是安装了下,由于目前还没有进行大的项目开发,暂时没有碰到坑。
使用了模块后,可以不用在GOPATH中再建立src目录了,直接在GOPATH中就行
另外,大部分的GO子命令都知道如何处理一个模块,如 run,get, build, install, list等,就是说如果存在go.mod文件。
你执行go run,go get ,go build....等会先去下载依赖的

模块初始化

go mod init gitlab.bytestar.io/grpc/grpc
以上命令在当前目录生成go.mod文件 ,只有一行 module gitlab.bytestar.io/grpc/grpcapi
这个文件不用手动维护,通过安装和删除依赖,会同步更新,维护好这个文件就行了
这个文件有一个替换功能,就是如果有被墙的包,可以用替换的方式。不像在GOPATH时候,自已需要手动去替换。

module gitlab.bytestar.io/grpc/grpcapi
replace (
    golang.org/x/text => github.com/golang/text v0.3.0
)

其它可参考帮助

go mod help

Usage:

    go mod <command> [arguments]

The commands are:

    download    download modules to local cache
    edit        edit go.mod from tools or scripts
    graph       print module requirement graph
    init        initialize new module in current directory
    tidy        add missing and remove unused modules
    vendor      make vendored copy of dependencies
    verify      verify dependencies have expected content
    why         explain why packages or modules are needed

在当前项目下,手动运行go mod tidy
这条命令会自动更新依赖关系,并且将包下载放入cache。在GOPATH/pkg/mod/下。

原文地址:https://www.cnblogs.com/smartrui/p/9974202.html