-bash:设置:-g:设置GOPATH时无效的选项

When I set my GOPATH use:

set -gx GOPATH /usr/local/Cellar/go/1.8.1

I get this issue:

-bash: set: -g: invalid option
set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]

The bash command set doesn't support the g option. Also this command is not used for setting environment variables all together - your snippet is probably intended for a different shell (fishshell?).

In bash, use export as suggested:

export GOPATH /usr/local/Cellar/go/1.8.1

However, you should understand what you are doing and how to configure your environment on MacOS (guessing from 'Cellar' in your path).

This might be a good starting point.

You should set GOROOT environment variable instead of GOPATH.

GOROOT should reference a folder (where go is installed), not the go executable itself

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

GOPATH should reference a folder under which you will find src, pkg and bin. (it should not reference directly the src folder): See "How to Write Go Code - Workspace"

Regarding the GOPATH:

  • try and set it in your ~/.bashrc (using export).
  • check that your current shell is a bash (and not another one like fish)
  • check the output of go env.