2

Trying to generate docs for my go package but not sure how to generate html docs

Tried with go doc but it just spits out the text version but I want html version.

looking at go help doc there is something that says about html go doc template.new but not sure how to use it.

Also tried with godoc, but unable to make it work.

installed it with

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

and it is present in the directory golang.org/x/tools/cmd

but when I try to run it using go tool godoc it throws an error saying:

go:no such tool godoc

and nothing in go bin folder that i can use.

Can anyone please tell, how can i make it work?

code0x00
  • 543
  • 3
  • 18
  • FYI the command is `godoc` not `go tool godoc`. – mkopriva Mar 30 '22 at 09:21
  • yes that's what i been finding everywhere, but whenever i run i always get `godoc: command not found` so i thought i should run `go tool godoc` as it is also in tools cmd directory. – code0x00 Mar 30 '22 at 09:34
  • @mkopriva Also in godoc README they says `This directory contains most of the code for running a godoc server. The executable lives at golang.org/x/tools/cmd/godoc.` but there is no executable there and not in go `bin` directory? – code0x00 Mar 30 '22 at 09:35
  • 4
    Probably because you haven't installed the tool. Use `go install`. In older versions the `go get` command used to also install tools but it doesn't anymore. – mkopriva Mar 30 '22 at 09:36
  • 3
    See https://pkg.go.dev/cmd/go#hdr-Add_dependencies_to_current_module_and_install_them *"In earlier versions of Go, 'go get' was used to build and install packages. Now, 'go get' is dedicated to adjusting dependencies in go.mod. 'go install' may be used to build and install commands instead."* and https://pkg.go.dev/cmd/go#hdr-Compile_and_install_packages_and_dependencies – mkopriva Mar 30 '22 at 09:38
  • @mkopriva thanks! it generated the binary in go `bin` directory after running `go install golang.org/x/tools/cmd/godoc`. Thanks a lot! – code0x00 Mar 30 '22 at 09:39
  • Just another thing, is `go doc` can produce html doc like `godoc` does as that is also the part of my question. If you can answer that, i'll really appreciate! – code0x00 Mar 30 '22 at 09:42
  • `go doc` cannot generate html. If it could it would say so in `go help doc`. – mkopriva Mar 30 '22 at 09:48
  • @mkopriva but in here: `https://pkg.go.dev/cmd/go/internal/doc` there is `go doc template.new Show documentation for html/template's New function. (html/template is lexically before text/template)` Can we use it somehow to generate html docs? – code0x00 Mar 30 '22 at 09:55
  • No, that's just an example of how you can show the documentation of a function in a package. `New` is the function and `html/template` is the pacakge. To show its documentation you can do `go doc html/template.new`. That's all that it says. It doesn't say anything about generating HTML output. – mkopriva Mar 30 '22 at 09:57
  • 2
    Does this answer your question? [How can I generate HTML documents using \`godoc\`?](https://stackoverflow.com/questions/13530120/how-can-i-generate-html-documents-using-godoc) – Jörg W Mittag Mar 30 '22 at 14:30

0 Answers0