无法在Mac OSX上设置$GOPATH。

huangapple go评论89阅读模式
英文:

Cannot set $GOPATH on Mac OSX

问题

我正在尝试将我的$GOPATH变量设置为在我的机器上运行一些示例代码:

$ smitego-example go run main.go 
main.go:5:2: 找不到包“github.com/#GITHUB_USERNAME#/smitego”的任何一个:
	/usr/local/go/src/pkg/github.com/#GITHUB_USERNAME#/smitego(来自$GOROOT)
	($GOPATH未设置)

$ smitego-example export $GOPATH=$HOME
-bash: export: `=/Users/#OSX_USERNAME#': 不是有效的标识符

github.com/#GITHUB_USERNAME#/smitego/smitego.go的内容:

package smitego

我该如何设置我的GOPATH,以便它始终有效?

英文:

I'm trying to set my $GOPATH variable to run some example code on my machine:

$ smitego-example go run main.go 
main.go:5:2: cannot find package "github.com/#GITHUB_USERNAME#/smitego" in any of:
	/usr/local/go/src/pkg/github.com/#GITHUB_USERNAME#/smitego (from $GOROOT)
	($GOPATH not set)

$ smitego-example export $GOPATH=$HOME
-bash: export: `=/Users/#OSX_USERNAME#': not a valid identifier

无法在Mac OSX上设置$GOPATH。

Contents of github.com/#GITHUB_USERNAME#/smitego/smitego.go:

package smitego

How can I set my GOPATH so it works always and forever?

答案1

得分: 147

更新,截至Go 1.8版本:如果你正在安装Go 1.8(发布于2017年2月)或更新版本,GOPATH将由Go工具链自动确定。在macOS(之前的OS X)上,默认为$HOME/go,例如/Users/matt/go/。这使得开始使用Go变得更加容易,你可以在安装Go后立即使用go get <package>

对于shell环境:(手动方法)

~/.bash_profile文件应包含export GOPATH=$HOME/goexport PATH=$GOPATH/bin:$PATH。使用$符号是重要的:请确保注意我使用了$符号的位置(以及我没有使用的位置)。

对于Sublime Text:

Sublime Text菜单 > Preferences > Package Settings > GoSublime > Settings: User

{
    "shell": ["/bin/bash"],
    "env": {"GOPATH": "/Users/#USERNAME#/go/"},
}

确保你的GOPATH没有设置为包的完整路径,只需设置为你的go文件夹的根目录,其中包含src、pkg和bin。如果你没有使用GoSublime,我建议你先安装它。

英文:

Update, as of Go 1.8: If you're installing Go 1.8 (released: Feb 2017) or later, GOPATH is automatically determined by the Go toolchain for you.

It defaults to $HOME/go on macOS (nee OS X) - e.g. /Users/matt/go/. This makes getting started with Go even easier, and you can go get &lt;package&gt; right after installing Go.


For the shell: (the manual method)

~/.bash_profile should contain export GOPATH=$HOME/go and also export PATH=$GOPATH/bin:$PATH. The use of the $ is important: make sure to note where I've used it (and where I have not).

For Sublime Text:

Sublime Text menu > Preferences > Package Settings > GoSublime > Settings: User

{
		&quot;shell&quot;: [&quot;/bin/bash&quot;],
		&quot;env&quot;: {&quot;GOPATH&quot;: &quot;/Users/#USERNAME#/go/&quot;},
}

Make sure your GOPATH is not set to the full path of the package; just the root of your go folder where src, pkg, and bin reside. If you're not using GoSublime, I'd suggest installing that first.

答案2

得分: 54

接受的答案对我没有起作用。我进行了调查并找到了原因:我正在使用zsh而不是bash。

我需要在~/.zshrc中添加以下两行:

export GOPATH=/Users/username/go
export PATH=$GOPATH/bin:$PATH
英文:

The accepted answer didn't work for me. I investigated and found the cause: I am using zsh, not bash.

I need to add the following two lines to ~/.zshrc:

export GOPATH=/Users/username/go
export PATH=$GOPATH/bin:$PATH

答案3

得分: 18

  1. 下载并安装Go工具
    https://golang.org/doc/install

  2. 设置Go工作空间

    mkdir $HOME/go &amp;&amp; cd $HOME/go
    mkdir bin pkg src

  3. 设置Go环境

    sudo vi ~/.bash_profile
    export GOPATH=$HOME/go
    PATH=$PATH:$GOPATH/bin

通过创建、构建和运行一个Go项目进行测试

mkdir -p $GOPATH/src/github.com/todsul/hello
touch $GOPATH/src/github.com/todsul/hello/hello.go
go install
hello
英文:
  1. Download and install Go tools
    https://golang.org/doc/install

  2. Setup Go workspace

    mkdir $HOME/go &amp;&amp; cd $HOME/go
    mkdir bin pkg src

  3. Setup Go environment

    sudo vi ~/.bash_profile
    export GOPATH=$HOME/go
    PATH=$PATH:$GOPATH/bin

Test by creating, building and running a Go project

mkdir -p $GOPATH/src/github.com/todsul/hello
touch $GOPATH/src/github.com/todsul/hello/hello.go
go install
hello

答案4

得分: 17

当你赋值变量时,不需要在变量前加上$前缀,只有在读取变量时才需要。

export GOPATH=$HOME

要使其永久生效,请将该命令放入你的.bash_profile文件中。

这将适用于终端shell。如果你需要设置会影响GUI应用程序的环境变量,请参考https://stackoverflow.com/questions/603785/environment-variables-in-mac-os-x/4567308#4567308。

英文:

You don't put the $ prefix on a variable when you're assigning it, only when you're reading it.

export GOPATH=$HOME

To make this permanent, put the command in your .bash_profile.

That will work for Terminal shells. If you need to set environment variables that will affect GUI applications, see https://stackoverflow.com/questions/603785/environment-variables-in-mac-os-x/4567308#4567308

答案5

得分: 5

http://www.golang-book.com/guides/machine_setup#osx只提供了在~/.bashrc中设置路径的说明,而没有提到~/.bash_profile。感谢这个帖子,我才能够成功构建我的示例文件。

export GOPATH=$HOME
export PATH=$PATH:$GOPATH/bin

其他Mac用户需要将上述内容添加到他们的~/.bash_profile中。

英文:

The http://www.golang-book.com/guides/machine_setup#osx

only has instructions for setting the path on ~/.bashrc, not ~/.bash_profile which thanks to this thread was able to get my example file to build.

export GOPATH=$HOME
export PATH=$PATH:$GOPATH/bin

Other Mac users need to add the above to their ~/.bash_profile.

答案6

得分: 5

安装Go后,可以使用brew或者包管理器来解决我的问题:

export GOROOT="/usr/local/go"
export GOPATH="$HOME/Documents/goWorkSpace"
export PATH="$HOME/Documents/goWorkSpace/bin:$PATH"
英文:

After installing go with brew or with package this solved my problem:

export GOROOT=&quot;/usr/local/go&quot;
export GOPATH=&quot;$HOME/Documents/goWorkSpace&quot;
export PATH=&quot;$HOME/Documents/goWorkSpace/bin:$PATH&quot;

答案7

得分: 1

在 macOS High Sierra 版本 10.3.3 上,已安装 Go,安装位置如下:

无法在Mac OSX上设置$GOPATH。

在 ~/.bashrc 中添加以下内容:

export GOPATH=/usr/local/go
export PATH=$PATH:$GOPATH/bin

然后 Go 就可以正常工作了。

无法在Mac OSX上设置$GOPATH。

英文:

on macOS High Sierra Version 10.3.3, Go Installed here :

无法在Mac OSX上设置$GOPATH。

Added following on :~/.bashrc

export GOPATH=/usr/local/go
export PATH=$PATH:$GOPATH/bin

and then Go Works

无法在Mac OSX上设置$GOPATH。

答案8

得分: -1

使用最新的Mac和Catalina版本的人们,你们需要更新.zshrc文件而不是.bash。

将以下两行添加到~/.zshrc文件中:

export GOPATH=/Users/username/go

export PATH=$GOPATH/bin:$PATH

应该可以正常工作!!

这个改变是一段时间前发生的,请参考下面的链接了解为什么使用.zshrc而不是.bash_profile
https://eshop.macsales.com/blog/56921-moving-from-bash-to-zsh-terminal-changes-in-macos-catalina/

英文:

People working with the latest macs and above Catalina version,
you guys need to update the .zshrc file instead of .bash.

Add the following two lines to ~/.zshrc:

export GOPATH=/Users/username/go

export PATH=$GOPATH/bin:$PATH

it should work.!!

This got change a while back, please refer to the link below to understand why .zshrc and not .bash_profile
https://eshop.macsales.com/blog/56921-moving-from-bash-to-zsh-terminal-changes-in-macos-catalina/

huangapple
  • 本文由 发表于 2014年2月1日 22:35:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/21499337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定