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
Related
The goal is to set up formatting for bash scripts (.sh files). So I'm trying to play around with the .sh formatter. From the project's github quick start section; I have go installed and have pulled in the shfmt module like so: GO111MODULE=on go get mvdan.cc/sh/v3/cmd/shfmt. But on trying to invoke the shfmt command. I'm getting the common command not found error. How do I actually use the shfmt command. My assumption is this is plug and play i.e. I don't need to actually go and fiddle around with PATHs or ENVs.
The command is installed as $HOME/go/bin/shfmt (unless GOBIN is set, then it's $GOBIN/shfmt):
$ go help install
usage: go install [-i] [build flags] [packages]
Install compiles and installs the packages named by the import paths.
Executables are installed in the directory named by the GOBIN environment
variable, which defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH
environment variable is not set.
The installation happens with a help of go install command as Peter mentioned. I would like to make a step by step that worked for me on Windows 7 machine, because I bet there are some of you who don't know anything about go language and don't even want to hear about it:
Install go language from golang.org (I used installer for Windows of course)
Download .sh formatter and unzip it somewhere
Navigate to the root directory ..\sh-master with your favourite terminal (I use GitBash or you can use cmd.exe which every Windows has by default)
Run command go install and installation should start
Once installation is done, through terminal (GitBash or cmd) navigate to $HOME/go/bin (on Windows by default it's under C:\Users\your_username\go\bin)
From here you can use the shfmt command like so shfmt -l -w yourBashScript.sh
Voila! now your bash script yourBashScript.sh is modified and formatted)
I am trying to install Go tools in my VS Code editor and when I select the View, CMD, Install and select Go: Install/Update tools, I get this -> "command 'go.tools.install' not found"
What am I doing wrong?
As seen in Microsoft/vscode-go issue 755:
For all guys who are stuck with command 'go.tools.install' not found problem on Windows.
Check if %GOPATH%\bin is in your PATH environment variable.
After half and an hour I finally figured out that PATH remains unchanged if you try to change it like set PATH=%PATH%.... You need explicitly change it in your system settings.
With recent Go installation, make sure GOROOT reference your Go installation folder.
I also like to set go.goroot to that folder in the settings of VSCode.
You don't need GOPATH: it defaults to %USERPROFILE%\go: make sure %USERPROFILE%\go\bin is in your PATH, before launching VSCode.
unzip go1.13.6.windows-amd64.zip anywhere (not in %USERPROFILE%\go, since it is reserved for GOPATH)
set GOROOT to C:\path\to\go (where you just uncompressed the Go archive)
add %GOROOT%\bin to %PATH%
In my case I was on Ubuntu and installed go using snap so I did the following:
Uninstall go from snap
sudo snap remove go
Download go from the original source: https://go.dev/doc/install
Install go as recommended in the source e.g.:
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.19.1.linux-amd64.tar.gz
Run the following to add the environment variables in the .bashrc:
echo export GOROOT=/usr/local/go >> ~/.bashrc
echo export GOPATH=\$HOME/go >> ~/.bashrc
echo export PATH=\$PATH:\$GOROOT/bin:\$GOPATH/bin >> ~/.bashrc
Note: Setting $GOROOT variable is obsolete in recent versions as discussed here
Restart the VS Code to read the environment variables set above
In VSCode press Ctrl+Shift+P and run again the GO: Install/Update tools, install everything selecting the checkboxes and you are good to GO!
In my case Go language was not installed so this command was not working from VS Code. Consider downloading and installing latest version of go language from https://go.dev/dl/
After I installed version 1.19.3 of go to my Windows 11 x64, the "go: install/Update Tools" command worked fine without any changes needed to be done to eviroment variables (PATH,GOROOT).(all changes were done by installer)
BTW restart of VSCODE really helps to make it work after installation of go.
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.
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.
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