运行程序时报go: cannot find main module, but found .git/config in
编写单元测试,运行时报下面的错误
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/base/cheshi01$ go test
go: cannot find main module, but found .git/config in /media/haima/34E401CC64DD0E28/site/go/src/haimait/learn
to create a module there, run:
cd ../.. && go mod init
出错原因是开启了go mod,但是没有初使化生成go.mod文件
解决方法一:
关闭gomod
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/base/cheshi01$ export GO111MODULE=off
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/base/cheshi01$ go env
GO111MODULE="off"
…… #省略
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/base/cheshi01$ go test
--- FAIL: TestAdd2 (0.00s)
cheshi01_test.go:16: result is wrong!
FAIL
exit status 1
FAIL haimait/learn/base/cheshi01 0.002s
解决方法二
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/base/cheshi01$ go mod init
go: creating new go.mod: module haimait/learn/base/cheshi01
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/base/cheshi01$ go mod tidy
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/base/cheshi01$ go test
--- FAIL: TestAdd2 (0.00s)
cheshi01_test.go:16: result is wrong!
FAIL
exit status 1
FAIL haimait/learn/base/cheshi01 0.002s
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/base/cheshi01$ go test -v
=== RUN TestAdd1
TestAdd1: cheshi01_test.go:11: result is right!
--- PASS: TestAdd1 (0.00s)
=== RUN TestAdd2
TestAdd2: cheshi01_test.go:16: result is wrong!
--- FAIL: TestAdd2 (0.00s)
FAIL
exit status 1
FAIL haimait/learn/base/cheshi01 0.002s
[Haima的博客]
http://www.cnblogs.com/haima/