a wooden desk topped with a laptop computer next to a monitor

解决Mac 上更新或重装golang后,执行go run 出现fork/exec /var/folders/ty/xxx/b001/exe/main: exec format error错误

最近升级了mac的Golang版本,当我在terminal执行go run main.go命令时,出现如下错误:

fork/exec /var/folders/ty/q11880gn5797hjv_tnk2m_k80000gn/T/go-build3935763930/b001/exe/main: exec format error

无效解决方案尝试:

0.官方给出的解决方案:

You are forcing Go use amd64 instead of arm64 by enviorment variable GOARCH="amd64".

go env -w GOARCH="amd64" go env -w GOARCH="arm64"均没有解决。

1.卸载Golang并重装:

rm -rf /usr/local/go 
rm -rf /etc/paths.d/go

正确解决方案:

查看 go env 配置后,发现了问题所在,

GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ty/q11880gn5797hjv_tnk2m_k80000gn/T/go-build3939751929=/tmp/go-build -gno-record-gcc-switches -fno-common"

重装或者更新go版本后,并没有在env里更新 /var/folders/ty/q11880gn5797hjv_tnk2m_k80000gn/路径。
卸载golang的同时删除go env:

% rm -rf /usr/local/go 
% rm -rf /etc/paths.d/go
% go env GOENV                                           
/Users/biandong/Library/Application Support/go/env
% cd /Users/biandong/Library/Application Support/
% rm -rf go

执行完成后,重新安装golang即可。

之后操作(可选):
开启GO111MODULE,如果已开启(即执行go env GO111MODULE结果为on)请跳过。
$ go env -w GO111MODULE="on"
设置GOPROXY
$ go env -w GOPROXY=https://goproxy.cn