升级Go版本后编译出错:Load redeclared in this block

今天打算把Go升级到1.14.2,升级的方法就两步,

先上Go官方下载页面( https://golang.org/dl/ )下载macos对应的二进制包(1.14.2地址 https://dl.google.com/go/go1.14.2.darwin-amd64.tar.gz

下载好后把压缩包解压到相应的目录:

1
$sudo tar -C /usr/local -xzf go1.14.2.darwin-amd64.tar.gz

因为是升级,不是初次安装,之前已经把PATH设置好了,就不用管了。

之后执行以下命令,查看Go版本:

1
2
$go version
go version go1.14.2 darwin/amd64

看到已经是1.14.2了,美滋滋。

但是用Go编译自己的程序,输出一大段如下错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# runtime/internal/atomic
/usr/local/go/src/runtime/internal/atomic/atomic_amd64x.go:18:6: Load redeclared in this block
previous declaration at /usr/local/go/src/runtime/internal/atomic/atomic_amd64.go:16:24
/usr/local/go/src/runtime/internal/atomic/atomic_amd64x.go:24:6: Loadp redeclared in this block
previous declaration at /usr/local/go/src/runtime/internal/atomic/atomic_amd64.go:22:32
/usr/local/go/src/runtime/internal/atomic/atomic_amd64x.go:30:6: Load64 redeclared in this block
previous declaration at /usr/local/go/src/runtime/internal/atomic/atomic_amd64.go:28:26
/usr/local/go/src/runtime/internal/atomic/atomic_amd64x.go:36:6: LoadAcq redeclared in this block
previous declaration at /usr/local/go/src/runtime/internal/atomic/atomic_amd64.go:34:27
/usr/local/go/src/runtime/internal/atomic/atomic_amd64x.go:41:6: Xadd redeclared in this block
previous declaration at /usr/local/go/src/runtime/internal/atomic/atomic_amd64.go:39:37
/usr/local/go/src/runtime/internal/atomic/atomic_amd64x.go:44:6: Xadd64 redeclared in this block
previous declaration at /usr/local/go/src/runtime/internal/atomic/atomic_amd64.go:42:39
/usr/local/go/src/runtime/internal/atomic/atomic_amd64x.go:47:6: Xadduintptr redeclared in this block
previous declaration at /usr/local/go/src/runtime/internal/atomic/atomic_amd64.go:45:47
/usr/local/go/src/runtime/internal/atomic/atomic_amd64x.go:50:6: Xchg redeclared in this block
previous declaration at /usr/local/go/src/runtime/internal/atomic/atomic_amd64.go:48:36
/usr/local/go/src/runtime/internal/atomic/atomic_amd64x.go:53:6: Xchg64 redeclared in this block
previous declaration at /usr/local/go/src/runtime/internal/atomic/atomic_amd64.go:51:38
/usr/local/go/src/runtime/internal/atomic/atomic_amd64x.go:56:6: Xchguintptr redeclared in this block
previous declaration at /usr/local/go/src/runtime/internal/atomic/atomic_amd64.go:54:45
/usr/local/go/src/runtime/internal/atomic/atomic_amd64x.go:56:6: too many errors

错误提示中说,atomic中的很多内容都重定义了,打开目录/usr/local/go/src/runtime/internal/atomic下的atomic_amd64x.goatomic_amd64.go,发现内容都差不多。

猜想可能新版本的源码文件名变更了,由于我们的新版本是直接解压过去的,导致老版本和新版本的源码文件同时存在。

尝试将/usr/local/go整个删除,再次sudo tar -C /usr/local -xzf go1.14.2.darwin-amd64.tar.gz安装新版本。

之后用Go编译程序就没有错误了。

/usr/local/go/src/runtime/internal/atomic目录看,只有atomic_amd64.go文件了,说明猜想正确。问题解决。

总结一下,安装Go时,如果选择安装在老版本目录,最好先把老版本完整删除掉。

本文完,作者yoko,尊重劳动人民成果,转载请注明原文出处: https://pengrl.com/p/20052/

0%