godoc command not found

Viewed 14836

godoc command doesn't work on my system (I'using Linux Mint 20 Ulyana).

I've just tried this procedure:

  1. install godoc with following command:

go get golang.org/x/tools/cmd/godoc

  1. 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?

5 Answers

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.

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.

Add $GOPATH/bin to your PATH variable. Executables, like godoc, are installed to $GOPATH/bin.

export PATH="$GOPATH/bin:$PATH"
godoc -http=:6060

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.

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

And open your browser at localhost:6060 to RTFM :-)

Related