command not found after go build

2020-07-15 09:58发布

问题:

I have install and setup go.

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

I have created a package at following location.

$HOME/go/src/github.com/shamsher31/gosymbol

I build inside package folder using

go build

It creates binary in bin folder inside GOPATH
But when I try to run package name from command line it gives following error.

symbol: command not found

How can I execute binary from command line ?

回答1:

You need following configuration for ubuntu.

$ sudo gedit ~/.bashrc

Add the following config

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

/usr/local/go/bin will be your go installation path and $GOPATH/bin will be where your custom build packages will get installed.



回答2:

On Windows:

1) Set $GOPATH env , e.g. $HOME/go
2) Add $GOPATH/bin into $PATH

It should work now.



回答3:

I was having a similar problem on OSX, I found this the easiest way to get golang up and running:

With HomeBrew:

brew install go

Then add these to your .bash_profile:

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


回答4:

for mac

example I put custom go folder in workspace directory. you could change my custom go directory workspace by your own.

add the following script to .bashrc

export GOROOT="/usr/local/go"
export GOPATH="$HOME/workspace/go"
export PATH="$HOME/workspace/go/bin:$PATH"

then run source .bashrc on your terminal



回答5:

For GO latest version go1.13.7 and above

If you have installed GO in its default location, then you no need to set up the GOROOT path. The default location for Unix or MAC is "/usr/local/go" and for Windows - "c:\Go"

You can verify the path using the command: go env

Note: If you are the getting the same error "command not found", then you need to unset GOROOT

If you want to set up GO in the preferred location, then you need to export the GOROOT path like this export GOROOT="/your/preferred/location" and export PATH="$PATH:$GOROOT/bin" in .bashrc or .bash_profile file.



标签: go