Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve documentation using godoc together with go modules?

It seems that the godoc tool is not Go modules aware.

A simple godoc -goroot=. serves the project files, but it does not generate documentation for the packages. I tested it from withing the projects source directory, where also the go.mod and go.sum module files are stored.

How to generate documentation for all packages inside a Go module - outside of $GOPATH?

In the release notes of Go 1.12 is written that the godoc tool will not be included in future Go releases and will only be available via go get after Go 1.12. One should use the Go go doc command. However, go doc does not generate "nice to read" HTML pages. Is there an alternative for documentation generation from Go source code which outputs HTML or Markdown?

like image 904
Simon Schürg Avatar asked Feb 28 '19 20:02

Simon Schürg


People also ask

What is Godoc in Golang?

Overview. Godoc extracts and generates documentation for Go programs. It runs as a web server and presents the documentation as a web page.

What is a Godoc?

Godoc is a Golang project tool that has been available to developers for nearly a decade. It parses Golang source code including comments and produces documentation as HTML or plain text.

What are the benefits of go module?

The first real benefit of Go modules is being able to add dependencies to your project in any directory and not just the GOPATH directory structure. You can also add packages to your module. In the next section, you will expand your module by creating an additional package within it.


2 Answers

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.

like image 135
stevegt Avatar answered Oct 10 '22 20:10

stevegt


Apparently it has been fixed here https://github.com/golang/go/issues/33655

All I had to do was to upgrade go to version 1.14 and then running godoc in my directory with go modules works.

The godoc command should run in module mode whenever the go command runs in module mode. So godoc should run go env GOMOD with the same environment and working directory, and interpret the result.

If the result of go env GOMOD is an empty string, then GOPATH mode is being used, and godoc will behave the way it used to with GOPATH and it will only look in the GOPATH directory.

like image 22
JFW Avatar answered Oct 10 '22 21:10

JFW