I've installed Go on my Linux machine in /usr/local/go
, and intended to use /usr/local/go/src
for development. However when I try this I get the following error:
$GOPATH must not be set to $GOROOT
Effectively as I understand it, this means you shouldn't use /usr/local/go
for development. Why not?
The GOPATH environment variableIf you would like to work in a different location, you will need to set GOPATH to the path to that directory.
You generally should not set GOROOT explicitly. The go command identifies the appropriate GOROOT automatically based on its own directory location. GOPATH defaults to $HOME/go . You only need to set it explicitly if you want to put it somewhere else.
GOROOT and GOPATH are environment variables that define this layout. GOROOT is a variable that defines where your Go SDK is located. You do not need to change this variable, unless you plan to use different Go versions. GOPATH is a variable that defines the root of your workspace.
Default GOPATH is: $HOME/go on Unix-like systems. %USERPROFILE%\go on Windows.
Because /usr/local/go/src
already contains the code for the standard library, and you should keep your own code separate from that.
I know, other development tools would have no problem with that, but Go is a little more strict in some ways. It's probably the same philosophy that lies behind flagging unused variables or imports as errors - avoiding problems which may seem small at first, but can lead to bigger headaches in the future.
Add following lines to your .bashrc
file:
export GOPATH="${HOME}/workspace" export GOROOT="${HOME}/go" export PATH="${GOPATH}/bin:${PATH"}
Then load the bashrc: $ source .bashrc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With