How to serve documentation using godoc together with go modules?

The issue isn’t modules so much as GOPATH. There’s a github issue thread that discusses this in more detail: https://github.com/golang/go/issues/26827

That thread has evolved a workaround that uses a docker container to run a godoc server with GOPATH set to the base of your dev tree. That godoc server will serve docs for all of the packages in your dev tree, regardless of whether they have a go.mod yet or not.

Here’s a version of the workaround that I just posted in that thread this morning — modify $devbase (or pass it in as $1) to point at the base of your tree:

#!/bin/bash 

set -x  # optional

devbase=$HOME/gohack
port=6060

docker run \
    --rm \
    -e "GOPATH=/tmp/go" \
    -p 127.0.0.1:$port:$port \
    -v $devbase:/tmp/go/src/ \
    --name godoc \
    golang \
    bash -c "go get golang.org/x/tools/cmd/godoc && echo http://localhost:$port/pkg/ && /tmp/go/bin/godoc -http=:$port"

You’ll note that I’m also using the gohack tool — it manages the ‘replace’ lines in go.mod for you so imports will find your local version of a module even if it’s not pushed to a server yet. There’s otherwise nothing special about $devbase — pointing it at $HOME/src should work just as well, for instance.

Leave a Comment