1. 程式人生 > >傻瓜式的go modules的講解和程式碼,及gomod能不能引入另一個gomod和gomod的use of internal package xxxx not allowed

傻瓜式的go modules的講解和程式碼,及gomod能不能引入另一個gomod和gomod的use of internal package xxxx not allowed

國內關於gomod的文章,哪怕是使用了百度 -csdn,依然全是理論,雖然golang的使用者大多是大神但是也有像我這樣的的弱雞是不是?

所以,我就寫個傻瓜式教程了。

程式碼很少很簡單。。。。

環境變數 GO111MODULE,有三個值on,off,auto,很好理解,不配置的話預設是auto

步驟

1.新建資料夾 go_moudules_demo

2.go mod之,生成go.mod檔案

go mod init go_moudules_demo
語法
go mod init [module]

3.建立main.go,預設包名是gomod,需要改成main

4.建立正真的存放程式碼的資料夾 demo和檔案gomod.go,注意不能與main放在同一資料夾下,因為會造成包名衝突

 5.根據規則引入程式碼,這裡有個坑,因為goland做的不太好,實際上golang的所有工具都做的不太好,導致程式碼報紅,但是實際上go build/run還是能跑通的

 

當然goland也可以配置

總結

gomod最容易讓人進了誤區就是,把自己之前的程式碼都gomod一次,那麼後面使用的時候直接根據gomod的package找之前的程式碼,簡直美滋滋。

畢竟是go moudules但是,實際上只是go moudule,他只管一個專案裡的多個包。

為什麼造成這個誤區呢?因為國內說的都是包管理,我還真以為是針對包的操作,然後第一次嘗試失敗後,翻了下官網

A module is a collection of related Go packages. 
Modules are the unit of source code interchange and versioning.
The go command has direct support for working with modules, including recording and resolving dependencies on other modules.
Modules replace the old GOPATH-based approach to specifying which source files are used in a given build.

 a collection of related Go packages. 相關Go包的集合,這玩意的理解真的是難,什麼相關,相關的是什麼?這時候根據官網的usage程式碼反向理解下go mod init [module],顯然是 module的相關Go包的集合,而module是一個單數啊。。。module和go mudules。。。我該如何理解啊。。。模板我倒是知道。。。總感覺這個怪不到谷歌頭上,而且這玩意大家試個兩下,就能找到正確理解也不算什麼事。而且我要是把自己的程式碼都丟到github上同樣不會報錯,只是我是想著不丟到github上面的使用所以進了歪路。

而第二句Modules are the unit of source code interchange and versioning.

Modules是原始碼的版本控制和交換的單位,也就說明go mod之間是獨立的,,,不能互調,除非在gopath裡面。感覺大神看到這句兩下都不用試了。。。

四 語法解析

主要是一個人的部落格 http://blog.51cto.com/qiangmzsx/2164520?source=dra

我把其中的關鍵抽出來,去掉他的程式碼,有興趣的可以去原文看看

    go mod init:初始化modules
    go mod download:下載modules到本地cache
    go mod edit:編輯go.mod檔案,選項有-json、-require和-exclude,可以使用幫助go help mod edit
    go mod graph:以文字模式列印模組需求圖
    go mod tidy:檢查,刪除錯誤或者不使用的modules,下載沒download的package
    go mod vendor:生成vendor目錄
    go mod verify:驗證依賴是否正確
    go mod why:查詢依賴

    go test    執行一下,自動導包

    go list -m  主模組的列印路徑
    go list -m -f={{.Dir}}  print主模組的根目錄
    go list -m all  檢視當前的依賴和版本資訊

 五 gomod import另一個gomod

似乎只能push到github或者使用類似gopath的方式,看了一圈沒有說gomod能import本地的gomod。

六 gomod遇到內部包的時候,報use of internal package xxxx not allowed

https://github.com/golang/go/issues/26446 解決方法。

同樣gay網大神指出

github.com/garyburd/redigo is not allowed to access github.com/gomodule/redigo/internal 
because github.com/gomodule/redigo is not a prefix of github.com/garyburd/redigo:this is working as designed. If you intend for github.com/garyburd/redigo to replace github.com/gomodule/redigo, you'll need to use a replace directive and import via the latter path.

也就是說要使用go mod edit -replace

go mod edit -replace=github.com/garyburd/[email protected]0.0=github.com/gomodule/[email protected]0.0

但是此時會報

go: github.com/gomodule/[email protected]0.0+incompatible used for two different module paths (github.com/garyburd/redigo and github.com/gomodule/redigo)

說真的我很想打人地說,又是你說要改成replace但是你現在只是換了個報錯,這麼敷衍的嗎?

然後,我想了想,換個版本號?

go mod edit -replace=github.com/garyburd/[email protected]0.0=github.com/gomodule/[email protected].0

程式成功執行!

這時候我發現了一句話

I'm planning to fix this before the final 1.11 release, but it might not make beta2.

go的大神說在1.11的最新版本之前會修復,但不在beta2(當前),此時我看到是9天前,而這bug整了我至少3天,,,

第一次追上大神的開發腳印?可喜可賀可喜可賀啊