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