无法编译Go文件-“初始化失败...在此块中重新声明了xxxx”

时间:2018-09-03 14:13:38

标签: go compilation

我是Go语言的新手,我遵循websiteyoutube video中的说明,当我运行go build hello.go时遇到以下错误:

go: disabling cache (/home/myuser/.cache/go-build) due to initialization failure: open /home/myuser/.cache/go-build/log.txt: permission denied
# runtime
/usr/local/go/src/runtime/map.go:64:2: bucketCntBits redeclared in this block
    previous declaration at /usr/local/go/src/runtime/hashmap.go:64:18
/usr/local/go/src/runtime/map.go:65:2: bucketCnt redeclared in this block
    previous declaration at /usr/local/go/src/runtime/hashmap.go:65:23
/usr/local/go/src/runtime/map.go:69:2: loadFactorNum redeclared in this block
    previous declaration at /usr/local/go/src/runtime/hashmap.go:69:18
/usr/local/go/src/runtime/map.go:70:2: loadFactorDen redeclared in this block
    previous declaration at /usr/local/go/src/runtime/hashmap.go:70:18
/usr/local/go/src/runtime/map.go:76:2: maxKeySize redeclared in this block
    previous declaration at /usr/local/go/src/runtime/hashmap.go:76:17
/usr/local/go/src/runtime/map.go:77:2: maxValueSize redeclared in this block
    previous declaration at /usr/local/go/src/runtime/hashmap.go:77:17
/usr/local/go/src/runtime/map.go:82:2: dataOffset redeclared in this block
    previous declaration at /usr/local/go/src/runtime/hashmap.go:85:4
/usr/local/go/src/runtime/map.go:91:2: empty redeclared in this block
    previous declaration at /usr/local/go/src/runtime/hashmap.go:91:19
/usr/local/go/src/runtime/map.go:92:2: evacuatedEmpty redeclared in this block
    previous declaration at /usr/local/go/src/runtime/hashmap.go:92:19
/usr/local/go/src/runtime/map.go:93:2: evacuatedX redeclared in this block
    previous declaration at /usr/local/go/src/runtime/hashmap.go:93:19
/usr/local/go/src/runtime/map.go:93:2: too many errors

它写了permission denied,但是我尝试用sudo go build运行它,但也失败了:

  

sudo:go:找不到命令

我正在使用Ubuntu 16.4。 我的转到文件hello.go$HOME/Desktop/go/src/hello中。

我的文件$HOME/myuser/.profile包含:
PATH="$HOME/bin:$HOME/.local/bin:$PATH:/usr/local/go/bin"

2 个答案:

答案 0 :(得分:1)

Gopath

The GOPATH environment variable lists places to look for Go code.
On Unix, the value is a colon-separated string. On Windows, the value 
is a semicolon-separated string. On Plan 9, the value is a list.

GOPATH must be set to get, build and install packages outside the standard Go tree.
Use tool like dep to segregate your different go project, maintaining your project dependencies. 

Goroot

 The Go binary distributions assume they will be installed in /usr/local/go (or c:\Go under Windows), but it is possible to install 
the Go tools to a different location. In this case you must set the 
GOROOT environment variable to point to the directory in which it was installed.

因此,始终首选使用自己的gopath来编译代码。 GOPATH用于您自己的go项目/第3方库(通过“ go get”下载)。

答案 1 :(得分:0)

重新安装Go解决了该问题。
发生问题是因为我在前一个版本的顶部安装了新版本的Go。

解决方案:
1. Remove Gorm -rvf /usr/local/go/
2.从Golang website重新安装:

cd ~/go/Downloads
// Depends on the version you download
tar -C /usr/local -xzf go1.14.4.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
cd ~/go/src/myproj/
go build
// no errors