在 Mac 上执行1.18构建错误: “ unix/syscall_darwin.1.13. Go: 25:3://Go: linkname 必须引用已声明的函数或变量”

我在 Mac 12 + 上升级到了 Go 1.18。

'go build' fails with errors like:

# golang.org/x/sys/unix
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/syscall_darwin.1_13.go:25:3:
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.1_13.go:27:3:
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.1_13.go:40:3:
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:28:3:
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:43:3:
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:59:3:
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:75:3:
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:90:3:
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:105:3:
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:121:3:
//go:linkname must refer to declared function or variable
../../gopath/pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_amd64.go:121:3:
too many errors


Compilation finished with exit code 2
36840 次浏览

这是由于旧版本的 golang.org/x/sys(在 这个问题中提到)引起的。修复方法是更新为:

go get -u golang.org/x/sys

正如 文件提到的,

  1. The easiest way : run get -u golang.org/x/sys.
  2. 手册: git 将存储库克隆到 $GOPATH/src/golang.org/x/sys

如果 go get -u golang.org/x/sys不起作用,请确保移除 go.mod中的这些线:

replace golang.org/x/sys => golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6

我也犯过同样的错误,我修正了下面的错误:

我先跑:

go get -u golang.org/x/sys

更新 x/sys库。

然后它打印了一些重要信息:

go: downloading golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e
go: upgraded golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd => v0.0.0-20220704084225-05e143d24a9e

拿着这个,我去了 go.mod,我在 go 1.14之前放了下面这行:

replace golang.org/x/sys => golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e

最后我跑了:

go mod vendor

Then it updated all the libraries:

go: downloading github.com/hashicorp/go-cleanhttp v0.5.1
go: downloading github.com/hashicorp/go-rootcerts v1.0.0
go: downloading github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
go: downloading github.com/mitchellh/cli v1.0.0
[...]
go: downloading github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db
go: downloading github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412
go: downloading github.com/Azure/go-autorest/autorest/to v0.4.0

然后就没事了

❯ go install .
❯ terraform --version
Terraform v0.13.5

编辑: Here I am using modules, if its not your case set GO111MODULE=off.