在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/下。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM