godoc command not found - go

godoc command doesn't work on my system (I'using Linux Mint 20 Ulyana).
I've just tried this procedure:
install godoc with following command:
go get golang.org/x/tools/cmd/godoc
Start godoc server:
godoc -http=:6060
The result is:
bash: godoc: command not found
I'm using this go version go version go1.15 linux/amd64
And this is my PATH variable /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/go/bin
All other go commands (go build, go run and so on) work correctly.
What can I do to make godoc command work?

I have a different issue. As of 1.18, you must now run
go install golang.org/x/tools/cmd/godoc
This is because go get is deprecated for
Starting in Go 1.17, installing executables with go get is deprecated. go install may be used instead. In Go 1.18, go get will no longer build packages...
In other words, go get in 1.18 and beyond will no longer install executables. Use go install.

Step - 1: Check if godoc package is installed
Make sure you can run godoc using this command:
$GOPATH/bin/godoc -http=:6060
Step - 2: Install godoc package
If you don't see any error then go to Step - 4 else if you can see this error No such file or directory then you have to get the godoc package first by using this command:
go get golang.org/x/tools/cmd/godoc
It will take some time to install.
Step - 3: Try godoc command
Try this command
godoc --help
if this command ran successfully then you are done and nothing else to do else if you are still getting any errors follow the Step - 4 and if you still fail please check if you have defined the $GOPATH variable correctly
Step - 4: Add path variable
Add $GOPATH/bin to your PATH variable by using this command:
export PATH="$GOPATH/bin:$PATH"
Try Step - 3 now.

Add $GOPATH/bin to your PATH variable. Executables, like godoc, are installed to $GOPATH/bin.
export PATH="$GOPATH/bin:$PATH"
godoc -http=:6060

Below is what I did on macos, it should work on linux as well.
Add this to your ~/.bashrc or ~/.zshrc:
export GOPATH=$HOME/go # or somewhere else
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
Reload your terminal and run:
mkdir -p $GOPATH $GOPATH/src $GOPATH/pkg $GOPATH/bin
go install golang.org/x/tools/cmd/godoc#latest
godoc -http=localhost:6060
Then you can open your browser at localhost:6060 to RTFM ;-)

I'm using macOS, for me $GOPATH was not configured and the path I found the installed package was $HOME/go/bin/godoc. Check out go help gopath for more information regarding this.

Related

zsh: command not found: mockgen - golang 1.18 - macOS Monterrey

I'm new to go. Currently I'm using zsh terminal in macOS, just followed the instructions pointed out here https://github.com/golang/mock when installing go mock. However when trying to execute a mockgen command I keep seeing zsh: command not found: mockgen and when navigating in the terminal to my $GOPATH/bin i see mockgen inthere, so I don't know if there's anything else needed.
These are the variables I have configured in my /.zshrc file:
#GO path
export GOPATH="$HOME/Documents/study_projects/go"
export GOBINPATH="$GOPATH/bin"
Idk if GOBINPATH is a proper name for this $GOPATH/bin variable to be exported to the PATH also, aso pointed out in https://github.com/golang/mock (mockgen/gomock) installation instructions. Does anyone of you know what else is needed here, do I need an additional configuration for this mockgen command to work with zsh?
Thank you!
Make sure run below for go 1.16+
go install github.com/golang/mock/mockgen#v1.6.0
Add users/[your_login]/go/bin to .zshrc path
add export PATH=$PATH:$(go env GOPATH)/bin in .zshrc and try again.

problem to start the local version of the golang tour

i installed go for ubuntu as explained here https://golang.org/doc/install and here https://tour.golang.org/welcome/3 ("When you run the tour program, it will open a web browser displaying your local version of the tour" but it does not)
works expected:
tar -C /usr/local -xzf go1.15.3.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
BTW i needed to usa a sudo prefix: sudo tar -C /usr/local -xzf go1.15.3.linux-amd64.tar.gz
that looks like expected:
go version
go version go1.13.8 linux/amd64
go get golang.org/x/tour
no go files listed (not expected):
then i tried go run tour.go
to open the local version of the tour in a web browser
and got this errors:
$ go run tour.go
go: downloading golang.org/x/tools v0.0.0-20190312164927-7b79afddac43
go: extracting golang.org/x/tools v0.0.0-20190312164927-7b79afddac43
go: finding golang.org/x/tools v0.0.0-20190312164927-7b79afddac43
# command-line-arguments
./tour.go:63:4: undefined: analyticsHTML
./tour.go:63:19: undefined: socketAddr
./tour.go:127:28: undefined: prepContent
m#kubuntu-MS-7C37:~/go/src/golang.org/x/tour$ go run
go run: no go files listed
go tool: no such tool "tour"
then i tired $ go tool tour got error go tool: no such tool "tour" ( https://github.com/golang/go/issues/24819#issuecomment-380777527 )
`
My Working Environment is Kubuntu 20.04.
After go get golang.org/x/tour, your tour binary will located at ~/go/bin.(here ~ means GO_HOME, default is your home folder)
then you can echo "export PATH=\$PATH:~/go/bin" >> ~/.bashrc && source ~/.bashrc
finally you get what you want below.
[x#localhost ~]$ tour
2020/10/26 14:37:17 Serving content from /home/x/go/src/golang.org/x/tour
2020/10/26 14:37:17 A browser window should open. If not, please visit http://127.0.0.1:3999
2020/10/26 14:37:18 accepting connection from: 127.0.0.1:55772
Use
~/go/bin/tour
We can write a go script, and run it with the go run.
Some languages can also be started like a script language e.g. kotlinc hello.kt.
That may not be so easy with go with tour. Better use the binary located at ~/go/bin .
Helping Links Writing Scripts with Go:
Go code as a script, gist: Writing Scripts with Go

Where does go get install packages?

I've been given instructions to run go get <some-remote-git-repo> which seems to succeed, but it's not clear to me where the package was installed to so I can run an executable from it.
Per https://golang.org/doc/code.html#remote it seems it will be installed in $GOPATH/bin but $GOPATH isn't defined in my shell (though the go get command seems to work fine). Go is installed via Homebrew.
I found the missing clue by running brew info go, which says:
==> Caveats
A valid GOPATH is required to use the `go get` command.
If $GOPATH is not specified, $HOME/go will be used by default:
https://golang.org/doc/code.html#GOPATH
From that I found the executable in question at $HOME/go/bin.
go env
you can get the path from GOMODCACHE
I had a similar problem which is why I found your question. However, in my case I did not have an empty GOPATH but one with multiple directories. I worked out the issue and describe it here...
If you run go get and you already have the package it says nothing (even with the -v option). This is confusing if it's not in the first directory of your GOPATH. Ie you run go get , there is no error or any message but when you check the first directory of the GOPATH (which is where the doc says it should be) you can't find it.
I eventually found it, but since I have a large GOPATH this was rather tedious.
Even if you don't have a $GOPATH variable you can see what it uses by doing go env GOPATH.
Try this to see what your path is:
echo `go env GOPATH`/bin
Using go build
Try this with main.go. In your greeter directory, run the following command:
go build
In this case, you built your greeter application into an executable file that was added to your current directory. Check this by running the ls command:
ls
./greeter

How to install "gotests" command?

I need to use the test driven development in Go using "gotests" command.
gotests -all *
This is not working. I did go get -u /github.com/cweill/gotests
and go install. But there is no binary created in $GOPATH/bin.
since there is NO main package, Use this command
$ go get github.com/cweill/gotests/...
this itself download all dependencies for the current package, and creates bin file, after downloading this package. see in $GOPATH/bin there will be a bin file named gotests
for more see HERE
The following worked for me with go v1.19.1
go install github.com/cweill/gotests/gotests#latest
Using go get to install things has been disabled since 1.18. See Deprecation of 'go get' for installing executables
go install github.com/rakyll/gotest
Source: https://github.com/rakyll/gotest

Golang equivalent of npm install -g

If I had a compiled Golang program that I wanted to install such that I could run it with a bash command from anywhere on my computer, how would I do that? For example, in nodejs
npm install -g express
Installs express such that I can run the command
express myapp
and express will generate a file directory for a node application called "myapp" in whatever my current directory is. Is there an equivalent command for go? I believe now with the "go install" command you have to be in the directory that contains the executable in order to run it
Thanks in advance!
Update: If you're using Go 1.16, this answer still works, but go install has changed and is now the recommended method for installing executable packages. See Karim's answer for an explanation: https://stackoverflow.com/a/68559728/10490740
Using Go >= 1.11, if your current directory is within a module-based project, or you've set GO111MODULE=on in your environment, go get will not install packages "globally". It will add them to your project's go.mod file instead.
As of Go 1.11.1, setting GO111MODULE=off works to circumvent this behavior:
GO111MODULE=off go get github.com/usr/repo
Basically, by disabling the module feature for this single command, it will install to GOPATH as expected.
Projects not using modules can still go get normally to install binaries to $GOPATH/bin.
There's a lengthy conversation and multiple issues logged about this change in behavior branching from here: golang/go - cmd/go: go get should not add a dependency to go.mod #27643.
Starting with Go >= 1.16 the recommended way to install an executable is to use
go install package#version
For example, go install github.com/fatih/gomodifytags#latest.
Executables (main packages) are installed to 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. You need to add this directory to your PATH variable to run executables globally. In my case, I've added this line to my ~/.zshrc file. (if you are using bash, add it to the ~/.bash_profile file):
export PATH="$HOME/go/bin:$PATH"
Go team published a blog post about this change, here's the explanation quote:
We used to recommend go get -u program to install an executable, but this use caused too much confusion with the meaning of go get for adding or changing module version requirements in go.mod.
Refer to go install documentation for more details
As far as I know, there is no direct equivalent to npm install -g. The closest equivalent would not be go install, but go get. From the help page (go help get):
usage: go get [-d] [-f] [-fix] [-insecure] [-t] [-u] [build flags] [packages]
Get downloads and installs the packages named by the import paths,
along with their dependencies.
By default, go get installs binaries to $GOPATH/bin, so the easiest way to make those binaries callable from everywhere is to add that directory to your $PATH.
For this, put the following line into your .bashrc (or .zshrc, depending on which shell you're using):
export PATH="$PATH:$GOPATH/bin"
Alternatively, you could also copy or link the executables to /usr/local/bin:
ln -s $GOPATH/bin/some-binary /usr/local/bin/some-binary
Short solution for Linux users:
Use the go get command as usual
Add the following lines to .bashrc:
# This is the default GOPATH, you should confirm with the 'go env' command
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Restart terminal or source it. Installed binaries will be available globally.
For Go v1.8+
go install package_name#latest
Caveat: this answer is outdated following the 2020 deprecation of go get. The solution presented here won't work with newer Go runtime installs.
The closest analogue of this in Go would be go get. By default, it will fetch a Go package from a supplied repository URL, and requires a $GOPATH variable to be set in your shell so that Go knows where to store the packages (and subsequently where to find them when compiling code depending on go get-ted packages).
Example syntax:
$ go get github.com/user/repo
The behaviour supplied by npm's -g flag is default, and packages installed using go get are normally available globally.
See go get --help for more information about the command.
As mentioned by #helmbert, adding your $GOPATH to your $PATH is useful if you're installing standalone packages.
if you are using zsh :
first: install your package using :
go install package#version
then , you edit your .zshrc file
nano ~/.zshrc
Add this line to the end of .zshrc file :
export PATH="$HOME/go/bin:$PATH"
last but not least :
source ~/.zshrc
then open a new terminal and execute your command :)
TL;DR at the bottom. I'm going to walk you through how I came to this conclusion, and why the more obvious solutions don't work.
Upon seeing this question, I thought "If I could set root's GOPATH=/usr, it would install things in /usr/bin/ and /usr/src!"
So I tried the obvious thing:
Add GOPATH=/usr to root's .bashrc.
And it worked!
Sort of.
Not really.
Turns out, sudo doesn't execute root's .bashrc. For "security" or something like that.
Do env_set or something in /etc/sudoers
Turns out, /etc/sudoers can only remove environment variables. There's no env_set directive.
(As far as I can find)
Dig through man sudoers.
Where does sudo get it's default set of environment variables from?
Well, the first one in the list is /etc/environment, so that's the one I used.
sudo echo "GOPATH=/usr" >> /etc/environment
sudo go get <repo>
Binaries will be put in /usr/bin, and sources will be put in /usr/src.
Running go as non-root will use GOPATH the "normal" way.
If you don't have go installed, you may use the gobinaries. it builds an on-demand binary of the project from github repo.
The command to install the go package would be:
curl -sf https://gobinaries.com/rakyll/hey | sh

Resources