Go common faqs
常见问题
1、 报错 404 Not Found
或 server response: not found: xxx.com/packagekit@v1
![](/images/go/simples/img.png)
设置公司私有依赖仓库
go env -w GOPRIVATE="g.xxx.com"
go env -w GONOPROXY="g.xxx.com"
go env -w GONOSUMDB="g.xxx.com"
2、 报错 wire: xxx: could not import xxx (invalid package name: “”)
![](/images/go/simples/img_0.png)
执行:go mod tidy
思路:先点蓝色部分,会跳到具体报错代码行,鼠标放到报错位置会提示具体报错问题
3、 报错 Windows环境执行generate时出现:google/protobuf/duration.proto: File not found.
![](/images/go/simples/img_1.png)
- 下载 protoc
- 把include整个放到GOPATH的bin下面
4、报错 package xxx is not in GOROOT (xxx)
![](/images/go/simples/img_2.png)
![](/images/go/simples/img_14.png)
思路:找到提示中提到的代码行,发现没跑generate,所以执行:go generate ./…
注意:每次跑完go generate都要看看api/service/v1/的pb.go文件是否新增或修改,没修改说明执行失败
5、 报错 missing go.sum entry for module providing ... go get github...
![](/images/go/simples/img_3.png)
思路:根据提示有建议执行的命令,拷贝粘贴执行即可,若存在多条则执行:go mod tidy
6、 Windows 服务替换程序 报错 Access is denied.
![](/images/go/simples/img_4.png)
文件夹权限问题,桌面新建文件夹,把exe和basesv文件夹放进去,执行exe
7、 报错 pkg\mod\github.com\go-kratos\xxx cannot use
或 has no field or method xxx
![](/images/go/simples/img_5.png)
go.mod 找到kratos,把版本改成v2.1.2
8、 docker环境报错 go: g.xxx.com/xxx ... git init --bare ... executable file not found in $PATH
![](/images/go/simples/img_6.png)
重新下载mod
9、 报错exec: “protoc”: executable file not found in %PATH%
![](/images/go/simples/img_7.png)
安装protoc
10、 报错 installing executables with ‘go get’ in module mode is deprecated... use ‘go get -d ‘
![](/images/go/simples/img_8.png)
原命令多加-d参数 或 换成go install github.com…
11、 报错 a connection attempt failed because ... host has failed to respond
![](/images/go/simples/img_9.png)
配置GOPROXY go env -w GOPROXY=https://goproxy.cn,direct
12、 docker环境跑成功后,本机访问不到
设置端口映射
13、 Goland的proto文件标红
![](/images/go/simples/img_10.png)
Goland下载插件Protocol Buffers
14、 报错 running "kratos": exec: "kratos": executable file not found in %PATH%
![](/images/go/simples/img_11.png)
安装kratos
go install github.com/go-kratos/kratos/cmd/kratos/v2
# go 1.16版本以上需要指定版本号或使用最新版
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
15、 运行golangci-lint run时,报错: File is not goimports
-ed with -local github.com/xxx (goimports)
执行:goimports -local “github.com/xxx” -w .
注意包名改成报错时提示的报名即可
16、 开发相关命令
- gocyclo检查圈复杂度
安装 go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
执行 gocyclo -over 10 ./
- go test 执行单元测试
执行 go test ./...
- go-carpet 单元测试覆盖率
安装 https://github.com/msoap/go-carpet#install
执行 go-carpet --summary
17、 Gorm相关
![](/images/go/simples/img_12.png)
![](/images/go/simples/img_13.png)
Gorm 插入表数据的时候 因为dba弄的自带的字段(time类型)没设置这个值,插入了000000
解决:加上配置&parseTime=True
原理: https://gorm.io/zh_CN/docs/connecting_to_the_database.html#MySQL(右图1)
https://github.com/go-sql-driver/mysql#parsetime(右图2)
类似场景:编码问题类似上面处理