Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate godoc documentation for an entire project?

Tags:

go

godoc

I've been wrestling with godoc and found that "go doc" is more for providing usage help from the command line for instance:

go doc -cmd -u

lists the package comment and any functions (or other entities)

go doc *function*

then shows the documentation for an individual function (or other entity)

It seems there is a related tool called godoc. godoc also seems to generate html on a per package and function basis. E.g.

godoc -html hello

Generates html containing the package comment only to stdout

godoc is a really confusing name given we have go doc as well!

How can I create static documentation for the whole project?

This is similar to Godoc, create html for entire package which may have been misinterpreted as asking about documentation for packages rather than projects. I want a build step I can use in a project that may in principle contain many packages and apps.

like image 502
Bruce Adams Avatar asked Nov 02 '18 19:11

Bruce Adams


People also ask

How do I run Godoc?

Running a Godoc Server Once you have commented your code, you can run the godoc command in your terminal, from your project's code directory. The command above hosts your code documentation on localhost, or 127.0. 0.1. The port doesn't have to 6060; godoc will run on any unoccupied port.

How do you write Godocs?

There are a few formatting rules that Godoc uses when converting comments to HTML: Subsequent lines of text are considered part of the same paragraph; you must leave a blank line to separate paragraphs. Pre-formatted text must be indented relative to the surrounding comment text (see gob's doc.go for an example).


2 Answers

is there a canonical way to generate documentation for offline use even using godoc?

Go 1.12 (February 2019) is clearer on that:

godoc and go doc

In Go 1.12, godoc no longer has a command-line interface and is only a web server.
Users should use go doc for command-line help output instead.

go doc now supports the -all flag, which will cause it to print all exported APIs and their documentation, as the godoc command line used to do.

cmd/doc: add -all flag to print all documentation for package

Unlike the one for the old godoc, you need the -u flag to see unexported symbols.
This seems like the right behavior: it's consistent.

like image 136
VonC Avatar answered Oct 02 '22 18:10

VonC


I was struggling to do this and finally, the thing that worked for me is

  1. make sure you have "wget" installed(I am using mac, so had to install it using x-code)

  2. log in as root user and modify the file called "robots.txt" to remove the line "Disallow : /" as this prevents wget to download the site recursively. The "robots.txt" file should be in $GOROOT path.

  3. open a cmd and start the godocs server using the below command

    godoc -http=:6060
    

    I have my local path configured to this port.

  4. Open another cmd and run the below command.

    wget -r -np -N -E -p -k http://localhost:6060/pkg/myproject
    

    you can mention the path of the project to have the html docs downloaded for entire project.

like image 3
braj Avatar answered Oct 02 '22 20:10

braj