18

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?

Alberto Morreale
  • 435
  • 1
  • 4
  • 11
  • 1
    Can you try `$GOPATH/bin/godoc -http=:6060` ? – jkr Aug 16 '20 at 22:11
  • Doing /home/myuser/go/bin/godoc -http=:6060 works. But I think I need to execute godoc inside my project directory to expose to localhost:6060 the specific project documentation. Am I right? Is there a way to execute commands without specifying absolute path? – Alberto Morreale Aug 16 '20 at 22:24
  • See `go help install` for the directories where Go looks for binaries. – Benjamin W. Aug 17 '20 at 02:54

5 Answers5

23

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.

Tom Anderson
  • 942
  • 8
  • 16
15

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.

princebillyGK
  • 2,917
  • 1
  • 26
  • 20
9

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

export PATH="$GOPATH/bin:$PATH"
godoc -http=:6060
jkr
  • 17,119
  • 2
  • 42
  • 68
  • 3
    If I'm not wrong, GOPATH is not used anymore. So in $HOME/.profile I added a new line according your suggestion using $HOME instead of $GOPATH ```export PATH=$PATH:/usr/local/go/bin``` ```export PATH=$PATH:$HOME/go/bin``` And now it works! Thank you very much! – Alberto Morreale Aug 16 '20 at 22:39
  • 1
    I had my `GOPATH=""`. So, I first had to set my GOPATH using `export GOPATH=/Users/myusername/go/` – Ruchit Patel Apr 19 '21 at 04:50
6

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.

Shubham Dhingra
  • 186
  • 3
  • 13
  • Plus-one for `go help gopath`, which led me to `go env GOPATH` to retrieve current `$GOPATH`! – ken Mar 07 '23 at 21:03
5

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 ;-)

DevonDahon
  • 7,460
  • 6
  • 69
  • 114