bash 如何修复我的 GOROOT 和 GOPATH 变量以运行 go?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36299375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:25:58  来源:igfitidea点击:

How do I fix my GOROOT and GOPATH variables to run go?

bashgoenvironment-variablesubuntu-14.04.profile

提问by Bangash

Operating system: Ubuntu 14.04

操作系统:Ubuntu 14.04

lines in the .bashrc (tried both of the following)

.bashrc 中的行(尝试了以下两种方法)

# GoPath (without goroot)
export GOPATH=$HOME/gowork

...

...

# GoPath (with goroot)
export GOROOT=/usr/local/go
export GOPATH=$HOME/gowork

Note: I tried to set the above environment variables, first in .profile and then in .bashrc as suggested by different people in a different way.

注意:我尝试设置上述环境变量,首先在 .profile 中,然后在 .bashrc 中,正如不同人以不同方式建议的那样。

Tried to run the following code

尝试运行以下代码

package main

import "fmt"

func main() {
    fmt.Println("Hello World!")
}

Error message in the terminal

终端中的错误信息

go run helloworld.go 
helloworld.go:3:8: cannot find package "fmt" in any of:
    /usr/local/go/src/pkg/fmt (from $GOROOT)
    /home/arif/gowork/src/fmt (from $GOPATH)
package runtime: cannot find package "runtime" in any of:
    /usr/local/go/src/pkg/runtime (from $GOROOT)
    /home/arif/gowork/src/runtime (from $GOPATH)

edit

编辑

go env output

去 env 输出

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/arif/gowork"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

go version output

去版本输出

go version go1.3.3 linux/amd64

A hint

一个提示

I remember that the tar I installed from was go1.6.linux-amd64.tar.gz, which means the go version 1.6 should be shown but my terminal is outputting 1.3.3.

我记得我安装的 tar 是 go1.6.linux-amd64.tar.gz,这意味着应该显示 go 版本 1.6 但我的终端输出的是 1.3.3。

回答by John Weldon

You should generally never have to set $GOROOTunless you know why you're setting it.

$GOROOT除非您知道为什么要设置,否则您通常不必进行设置。

This assumes a 'normal' installation of Go. If you've moved the go binary around, or deleted pkg or src folders, your mileage may vary.

这假设 Go 的“正常”安装。如果你移动了 go 二进制文件,或者删除了 pkg 或 src 文件夹,你的里程可能会有所不同。

Also, make sure an older version of go is not found in your $PATH before the version you intend to use.

另外,请确保在您打算使用的版本之前在 $PATH 中找不到旧版本的 go。

In this case @Bangash had to delete the old /usr/bin/go and add /usr/local/go/bin to his PATH environment variable.

在这种情况下,@Bangash 必须删除旧的 /usr/bin/go 并将 /usr/local/go/bin 添加到他的 PATH 环境变量中。

Usually people use $GOROOTwhen switching between multiple versions of go.

通常人们在 go 的多个版本之间切换时使用 $GOROOT

Please read comments as well as chat for the solution.

请阅读评论以及聊天以获取解决方案。