package main
import "fmt"
func main() {
fmt.Println("Hello world!")
}
上述代码报错:
could not import fmt (cannot find package "fmt" in any of /usr/local/Cellar/go/1.15.7/libexec/src/fmt (from $GOROOT) /Users/bytedance/go/src/fmt (from $GOPATH))二、问题原因:
GOROOT配置错误:
如图可以看到我的 GOROOT 路径和上文报错路径不一致,可能是安装的时候有些混乱,所以需要按照报错提示重新配置一下。
GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/Users/bytedance/Library/Caches/go-build" GOENV="/Users/bytedance/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOINSECURE="" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/bytedance/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="off" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/jr/w6vq2s9x43340qv18sc7zlz00000gp/T/go-build690732439=/tmp/go-build -gno-record-gcc-switches -fno-common"三、解决办法:
打开终端,按下述方法修改 GOROOT 配置:
vim ~/.bash_profile i
esc :wq source ~/.bash_profile go env
GO111MODULE="" GOARCH="amd64" GOBIN="/Users/bytedance/go/bin" GOCACHE="/Users/bytedance/Library/Caches/go-build" GOENV="/Users/bytedance/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOINSECURE="" GOMODCACHE="/Users/bytedance/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/bytedance/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/Cellar/go/1.15.7/libexec" GOSUMDB="off" GOTMPDIR="" GOTOOLDIR="/usr/local/Cellar/go/1.15.7/libexec/pkg/tool/darwin_amd64" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/jr/w6vq2s9x43340qv18sc7zlz00000gp/T/go-build286606926=/tmp/go-build -gno-record-gcc-switches -fno-common"参考资料:
https://dave.cheney.net/2013/06/14/you-dont-need-to-set-goroot-really



