command not found after go build - go

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 ?

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.

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

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 macOS 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.

i was the same problem in mac but i'm using zsh
I was able to solve the problem by editing the file ~/.zsh
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go;
export PATH=$PATH:$GOPATH/bin;

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

as icza said
go build leaves the executable in the current working directory.
Maybe help add ./ to you run command.
go build
./symbol

On Windows:
Set $GOPATH env, e.g., $HOME/go
Add $GOPATH/bin into $PATH
It should work now.

Related

Cannot Install Delve Go Debugger on Mac

I am trying to follow along with the following YouTube video on getting started with Go Debugging.
It recommends following the Delve installation instructions on the official Delve github repo. For Mac users, they are as follows:
Making sure the toolchain is in place
xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
Using "go get" to install Delve
go get -u github.com/go-delve/delve/cmd/dlv
Making sure developer mode is enabled in Xcode
sudo /usr/sbin/DevToolsSecurity -enable
Developer mode is already enabled.
To check that the installation has been completed correctly, I tried running the following in my Go project directlry:
dlv debug
zsh: command not found: dlv
The author of the video tutorial recommends updating GOPATH and PATH variables in the ~/.bash_profile file in the case that the command is not recognized. I did so so by adding:
export GOPATH=/Users/<user_name>/go/src/
export PATH=$PATH:/Users/<user_name/go/src/my_project
However, even after doing so, I get the same result when trying to run the debugger:
dlv debug
zsh: command not found: dlv
Even if I change the shell for the default zsh to bash, using exec bash, I get the same result.
In order to run an executable, it needs to be available in your PATH.
1. Configure your path.
Make sure that your GOPATH and $GOPATH/bin directories are set correctly in your shell environment. You can do this by adding the following lines to your shell configuration.
~/.zshrc if you're using zsh.
~/.bash_profile if you're using bash.
export GOPATH="$HOME/go"
export PATH="$GOPATH/bin:$PATH"
2. Re-load your shell configuration.
Make sure to either restart your shell or run source on your shell configuration file after the changes:
source ~/.zshrc if you're using zsh.
source ~/.bash_profile if you're using bash.
3. Install the dlv package.
go install github.com/go-delve/delve/cmd/dlv
This is assuming that you're using /Users/<username>/go as your GOPATH.
You should now be able to run dlv from your terminal session.
Good luck!
Set the environment variable GOBIN to be where you want the dlv binary to be installed.
For example:
GOBIN=~/bin go install github.com/go-delve/delve/cmd/dlv
This will install dlv in ~/bin
Clarification
When you run go install, the installation path can be specified by setting the GOBIN environment variable.
There are two ways to set the environment variable:
1) Run export GOBIN=<SOMETHING> before you run go install ..
$ export GOBIN="$HOME/bin"
$ go install github.com/go-delve/delve/cmd/dlv
The export command will alter the environment in the current terminal session. Any later command you execute will see the value you set for GOBIN
When you go with this approach, you usually want to have this environment variable active not only in this session, but all future sessions as well. So it's better to add the line to your bash profile.
2) Set the environment variable only for the command.
$ A=10 some-command
In this case, some-command will see the value of the environment variable A set to '10'. If you run a later command, it will not see this value.
This approach is useful when you are just trying things out, or if you only want to set certain environment variables in certain situations.
The command line I provided as the answer follows this second approach.
It sets the GOBIN variable to the ~/bin directory, and then invokes go install in the same line. This way, this invocation of go install will install dlv in ~/bin
This of course assumes you have a bin directory in your home directory.
If you don't have such directory, then this will not work.
The idea is not to copy paste the line as is. The idea is to change ~/bin to be the directory where you would like the dlv binary to be installed.

Golang installation

I just followed the installation guide for golang (ubuntu 16).
I extracted the archive at /etc/usr
I added env variable in /home/user/.profile
I just tested a basic go build on the hello world code.
I get the following error:
The program 'go' is currently not installed. You can install it by typing: sudo apt install golang-go
Why does it ask me to install it (again?)?
open the go documentation download
https://go.dev/dl/
choice your os and go version
download then extract the file
extract the file
open the file and open the terminal
6.Add /usr/local/go/bin to the PATH environment variable.
export PATH=$PATH:/usr/local/go/bin
then check the go version
go version
The location of the binary go is not in your path. Ubuntu does not find it and suggests to install it. Add this line to your file /etc/profile, or better $HOME/.profile:
export PATH=$PATH:/usr/local/go/bin
This is documented in the docs: https://golang.org/doc/install#install
If you want to try this solution before editing any files, you can just execute the above command and try to execute the go command in the shell.
There are paths which needs to be set correctly for you go installation to work
GOROOT points to directory where go is installed
export GOROOT=/usr/lib/go
GOPATH points to you workspace directory
export GOPATH=$HOME/go
These paths need to be added in global path variable.
export PATH=$PATH:$GOROOT/bin
You need to put the go executable in your system path. which you can do by
export PATH=$PATH:/etc/usr/go/bin
You can put the same in /home/user/.profile
just use asdf for installation. You can have several version also :D
Docs: https://asdf-vm.com/#/core-manage-asdf
downlaod the installer form enter link description here, choose intaller for linux that suit your device and then you go to your CLI and use wget or curl :
$ wget https://storage.googleapis.com/golang/go1...
and then extract the file to /usr/local :
$ tar -C /usr/local -xzf go1...
add path binary Go to PATH environment variable :
$ echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc
$ source ~/.bashrc
and then use go version to check if the Go already installed
If You are using linux then open your terminal and run this command.
sudo apt install golang-go
This command will Install Go lang. in your system. ThankYou
Steps for Go installation:
sudo apt-get update && sudo apt-get -y upgrade    
wget https://dl.google.com/go/go1.17.5.linux-amd64.tar.gz
sudo tar -xvf go1.17.5.linux-amd64.tar.gz
sudo mv go /usr/local/
export GOROOT=/usr/local/go
Add in .bashrc
vi .bashrc
export GOPATH="/root/go"
export GOROOT=/usr/local/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
Download latest version from https://golang.org/doc/install
tar -xzf go1.15.7.linux-amd64.tar.gz
move to /usr/lib/ to folder with version number
sudo mv go /usr/lib/go-1.15
create symkink link on /usr/bin/
ln -s /usr/lib/go-1.15/bin/go /usr/bin/go

Failed to localise android-ndk installed from homebrew

I have installed SDK doing brew cask install android-ndk and NDK doing brew cask install android-ndk, everything was ok during the installation and inserted the PATH, like this:
export GRADLE_HOME=/usr/local/opt/gradle
export ANDROID_HOME=/usr/local/Caskroom/android-sdk/3859397
export ANDROID_NDK_HOME=/usr/local/Caskroom/android-ndk/16b/android-ndk-r16b
export ANDROID_SDK_ROOT=/usr/local/Caskroom/android-sdk/3859397
export PATH=$GRADLE_HOME/bin:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
Saved and committed doing source ~/.bash_profile.
After, I tried to run my gradle task, during start of compile of a C++ library, I got this:
* What went wrong:
A problem occurred configuring project ':some-library'.
> NDK not configured.
Download it with SDK manager.
Is there any way to solve it?
Thank you.
In macOS installed with brew use this the following paths
In file ~/.bash_profile
export ANDROID_NDK_HOME = /usr/local/share/android-ndk
export ANDROID_HOME = /usr/local/share/android-sdk
If you are using another bash script or zsh for example, you should put it begin of that script file, eg.
myortherscript.sh
#!/bin/bash
# THIS AT TOP
source ~/.bash_profile
Important: This sample I'm considering the same user for configuration bash_profile and script execution.

godep: exec: "go": executable file not found in $PATH?

I am trying to run make run. But getting this. I do not know to how to set this.
You need to set the path variable to where your go package is installed. Typically on ubuntu is installed on the /bin folder. So in your case you have to set it to:
$ export PATH=$PATH:$GOPATH/bin
Check the documentation here: https://golang.org/doc/code.html

How to set GOPATH in Mac OS X 10.10

I installed Go 1.4 in Mac OS X. Previously I had Go 1.0. I set the GOROOT and PATH as follows,
Dineshs-MacBook-Air:go-cassandra Dany$ which go
/usr/local/go/bin/go
Dineshs-MacBook-Air:go-cassandra Dany$ export GOROOT=/usr/local/go/bin/go
Dineshs-MacBook-Air:go-cassandra Dany$ export PATH=$PATH:$GOROOT/bin
Go is installed in '/usr/local/go/bin/go'. And I set the GOPATH as my project src directory. I am able to run go code inside my directory. But when I try to install gocql I am getting error.
Dineshs-MacBook-Air:go-cassandra Dany$ sudo go get github.com/gocql/gocql
package github.com/gocql/gocql: cannot download, $GOPATH not set. For more details see: go help gopath
Could anyone help me on this? Thank you
EDIT 1: #VonC I tried the other option as well. I changed the GOROOT to the directory where go is installed. But it didn't help. And I changed the GOPATH.
Dineshs-MacBook-Air:go-cassandra Dany$ export GOROOT=/usr/local/go
Dineshs-MacBook-Air:go-cassandra Dany$ export PATH=$PATH:$GOROOT/bin
Dineshs-MacBook-Air:go-cassandra Dany$ export GOPATH=/Users/Dany/Documents/FALL-2013-COURSES/Imp_Data_structures/workspace/go-cassandra
Dineshs-MacBook-Air:go-cassandra Dany$ sudo go get github.com/gocql/gocql
Password:
package github.com/gocql/gocql: cannot download, $GOPATH not set. For more details see: go help gopath
Dineshs-MacBook-Air:go-cassandra Dany$ echo $GOPATH
/Users/Dany/Documents/FALL-2013-COURSES/Imp_Data_structures/workspace/go-cassandra
Dineshs-MacBook-Air:go-cassandra Dany$ ls
bin pkg src
Dineshs-MacBook-Air:go-cassandra Dany$
Notes:
GOROOT should reference a folder (where go is installed), not the go executable itself
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
As Dave mentions in the comments, you should not have to set GOROOT at all in your case.
See the article You don’t need to set GOROOT, really.
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.
Don't do a sudo go get, as the environment variable used for sudo (root) wouldn't be the same as the current user:
go get github.com/gocql/gocql
(or you would need to do a sudo -E bash -c 'go get github.com/gocql/gocql', but I suspect you don't need root here)
See sudo caveat:
Any variables added to these locations will not be reflected when invoking them with a sudo command, as sudo has a default policy of resetting the Environment and setting a secure path (this behavior is defined in /etc/sudoers)
You will need to inform to Go the location of your workspace. In this example we gonna use $HOME/dev/go-workspace.
Then you need to know if your mac have zsh or bash configured as shell.
The file ~/.zshrc is used for zsh shell. The zsh shell was introduced
in OS Big Sur.
The ~/.bashrc is the bash shell used in previews OS version, the same for linux users.
1: Add those lines to export the required variables in your ~/.zsh or ~./bashrc depending on your shell.
For go installed from original pkg download from https://golang.org/doc/install
export GOPATH=$HOME/dev/go-workspace
export GOROOT=/usr/local/go
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
For Go installed from home brew. (brew update and brew install golang)
export GOPATH=$HOME/dev/go-workspace
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
2: run
# source ~/.zshrc
or
source ~./bashrc
to update your $PATH according with the new variables inserted in step #2
3: Then create your the workspace directories:
$ mkdir -p $GOPATH $GOPATH/src $GOPATH/pkg $GOPATH/bin
4: create a test.go, the hello world
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
Run your program by executing:
$ go run test.go
If you wish to compile it and move it to $GOPATH/bin, then run:
$ go install test.go
Since we have $GOPATH/bin added to your $PATH, you can run your program from anywhere just typing test :
$ test
If everything is right the output will be :
hello, world
tldr;
macos terminal:
brew install go
edit ~/.zshenv
export GOROOT="/usr/local/Cellar/go/1.16.6/libexec"
export GOPATH="/Users/Shared/Development/go-workspace"
export PATH="$PATH:$GOPATH/bin"
Think of the GOPATH in terms of a configured workspace for the environment. GOROOT is the install point for the go language. Homebrew will symlink the bin commands to your /usr/local/bin. Add the $GOPATH to your PATH in order to find any go programs that you later install.
I face many issues to set the GOROOT and GOPATH in MacOS, and the only solution that worked for me, is the below one:
Add the following code into your ~/.bashrc or ~/.profile if you use Oh My Zsh ~/.zshrc
export GOPATH=$HOME/go
export GOROOT=/usr/local/go
After that, run the following command in your terminal:
source ~/.profile
If you have any different shell, replace it in the place of .profile.
Info: $HOME variable will point to your current user's directory.
Info 2: /usr/local/go is where all users, the go files, and bin folder exists.
To Set Custom GOPATH as working directory:
To store your projects of GoLang in different folders, you can set the custom path. Here is my custom GOPATH, which points to my goWorkSpace folder in my admin/Documents folder.
export GOPATH=$HOME/Documents/goWorkSpace
You're setting $PATH incorrectly. $GOROOT is set and added to the $PATH during installation. So neither:
export GOROOT=/usr/local/go/bin/go
export PATH=$PATH:$GOROOT/bin
nor:
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
is needed because $GOROOT will be different based on how you've installed Go - Homebrew, go.dev, etc.
You just need to set $GOPATH.
If you find XDG Base Directory Specification useful (this will keep your $HOME clean), you could:
Create a user-specific data directory
mkdir -p $HOME/.local/share/go
Set $GOPATH in .zshenv
export GOPATH="$HOME/.local/share/go"
export PATH="$PATH:$GOPATH/bin"
Refresh environment variables
source .zshenv
# or quit and re-open the shell
Install Go package
go install golang.org/x/tools/gopls#latest
Verify version of package
which gopls

Resources