go開發遇到的問題


1. go mod tidy : dial tcp xx:443: i/o timeout,

因為go 1.17 默認的GOPROXY=https://proxy.golang.org,direct ,在牆內無法訪問。

所以在.zshrc /.bashrc 加上環境變量

export GOPROXY=https://goproxy.io
export GO111MODULE=on

 

2. go1.17. GoLand 2021.2.3. 因為有些舊的依賴包,不希望啟用go module. 在GOPATH下設置了路徑,把依賴包放到了GOPATH的src下,Go Modules下的Enable Go modules integration也沒打上勾,但go build時報

 
         

package common/crypto is not in GOROOT (/usr/local/go/src/common/crypto)

handler.go:4:2: package common/db is not in GOROOT (/usr/local/go/src/common/db)

 但GOPATH已設置, 而且也沒啟用module模式

go env GO111MODULE 發現是auto,想改成off,用命令

go env -w GO111MODULE=off

但下面出了警告

warning: go env -w GO111MODULE=... does not override conflicting OS environment variable

在GoLand的控制台打go env GO111MODULE 發現還是auto,但在mac的控制台下go env GO111MODULE 是off

此時要把module模式改成off們需要在.zshrc上加上

export GO111MODULE=off

 

3.如果本地有個common module,希望引入本地common, 需要加

cd common
go mod init common

 

cd hallServer
go mod edit -replace common=../common
go mod tidy

表示common這個module就是../common

此時打開go.mod, 有一條common是個虛擬的版本號

5 require (
  6     common v0.0.0-00010101000000-000000000000
  7     github.com/gin-gonic/gin v1.7.4
  8 )

 

4. go 1.17.   

問題描述:

common 包要依賴github.com/jb00007 ,但在common包 go mod tidy時

common/tcp imports
    github.com/jb00007/common/log: cannot find module providing package github.com/jb00007/common/log: module github.com/jb00007/common/log: git ls-remote -q origin in /Users/gaoxianghu/go/pkg/mod/cache/vcs/48e703907eff3f5c3f402971dfd15fbaf97b52b1b7cf00f1fdb40daa9dac5325: exit status 128:
    fatal: could not read Username for 'https://github.com': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
common/tcp imports
    github.com/jb00007/common/util: cannot find module providing package github.com/jb00007/common/util: module github.com/jb00007/common/util: git ls-remote -q origin in /Users/gaoxianghu/go/pkg/mod/cache/vcs/48e703907eff3f5c3f402971dfd15fbaf97b52b1b7cf00f1fdb40daa9dac5325: exit status 128:
    fatal: could not read Username for 'https://github.com': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
common/jbcoder tested by
    common/jbcoder.test imports
    github.com/jb00007/common/tcp: cannot find module providing package github.com/jb00007/common/tcp: module github.com/jb00007/common/tcp: git ls-remote -q origin in /Users/gaoxianghu/go/pkg/mod/cache/vcs/48e703907eff3f5c3f402971dfd15fbaf97b52b1b7cf00f1fdb40daa9dac5325: exit status 128:
    fatal: could not read Username for 'https://github.com': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
common/tcp tested by
    common/tcp.test imports
    github.com/jb00007/common/jbcoder: cannot find module providing package github.com/jb00007/common/jbcoder: module github.com/jb00007/common/jbcoder: git ls-remote -q origin in /Users/gaoxianghu/go/pkg/mod/cache/vcs/48e703907eff3f5c3f402971dfd15fbaf97b52b1b7cf00f1fdb40daa9dac5325: exit status 128:
    fatal: could not read Username for 'https://github.com': terminal prompts disabled
Confirm the import path was entered correctly.

好像是無法在github.com下載 github.com/jb00007 里的東西,只能用本地的jb00007包, 於是

cd jb00007
go mod init github.com/jb00007
go mod tidy

cd ../common
go mod edit -replace github.com/jb00007=../jb00007
go mod tidy

其中在jb00007中go mod tidy時報:

go: github.com/jb00007/jbserver2/KenoServer imports
    github.com/googleapis/google-cloud-go/civil: github.com/googleapis/google-cloud-go@v0.97.0: parsing go.mod:
    module declares its path as: cloud.google.com/go
            but was required as: github.com/googleapis/google-cloud-go

但對common的go mod tidy沒影響,所以也沒管這個問題,但當我 到hallServer下執行go mod tidy時就報下面

cd hallServer
go mod tidy

o: downloading golang.org/x/net v0.0.0-20211008194852-3b03d305991f go mod tidy: error loading go 1.16 module graph: common@v0.0.0-00010101000000-000000000000 requires github.com/jb00007@v0.0.0-00010101000000-000000000000: invalid github.com import path "github.com/jb00007"

If reproducibility with go 1.16 is not needed:

go mod tidy -compat=1.17

 

這問題是loading了工1.16的module graph,但我是1.17呀,於是照着說明運行  go mod tidy -compat=1.17 就ok了。

 ========

上面的項目在蹈入Goland時會有很多包沒下下來,可能是因為jb00007 go mod tidy沒有成功的關系,因為jb00007依賴的包找不到。把jb00007目錄刪除,common里引用到jb00007里的代碼先注掉,然后把common 的 mod包引用到jb00007的都刪掉,然后在common下重新go mod tidy,這次成功了。然后把hallServer,pushServer,subGame/zjh,subGame/rummy, subGame/nn,subGame/hhdz重新go mod tidy, go build,成功。重新在Goland打開項目,發現包都可以找到了。

在go mod tidy 時,有時會發生:

subGame/nn imports
    common/db imports
    github.com/denisenkom/go-mssqldb imports
    cloud.google.com/go/civil tested by
    cloud.google.com/go/civil.test imports
    github.com/google/go-cmp/cmp loaded from github.com/google/go-cmp@v0.2.0,
    but go 1.16 would select v0.5.5

可能是在本地找到的低版本的包(go-cmp@v0.2.0),但建議的是更高的版本(go-cmp@v0.5.5),

按照建議執行go mod tidy -go=1.16 && go mod tidy -go=1.17即可

To upgrade to the versions selected by go 1.16:
    go mod tidy -go=1.16 && go mod tidy -go=1.17
If reproducibility with go 1.16 is not needed:
    go mod tidy -compat=1.17
For other options, see:
    https://golang.org/doc/modules/pruning

 


免責聲明!

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



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