go - 无法编译 Go 文件 - "initialization failure...xxxx redeclared in this block"

标签 go compilation

我是 Go 的新手,我按照 website 中的说明进行操作和 youtube 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: command not found

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

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

最佳答案

路径

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. 

去根

 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”下载)。

关于go - 无法编译 Go 文件 - "initialization failure...xxxx redeclared in this block",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52151503/

相关文章:

go - 如何使用 gorm 为表编写注释?

c++ - 错误 : expected ‘,’ or ‘...’ before ‘for’ in #define

Java运行时编译

python - 在 Python 脚本中的 Windows 中的 C++ 命令行编译

go - 如何在Golang Revel框架下运行角度构建

go - “标题”注释的目的

c++ - 在 C++ 中编译 opencv

java - 解决静态方法歧义

go - 在 proto 消息中声明一个字段标签

javascript - Golang 和 JavaScript 模块