import godoctricks "github.com/fluhus/godoc-tricks"
Package godoctricks is a tutorial on the features of GoDoc. It is meant to help eveyone make the most out of this feature of Go.
Notice that this doc is written in godoc itself as package documentation. The defined types are just for making the table of contents at the head of the page; they have no meanings as actual types.
If you have any suggestion or comment, please feel free to open an issue.
By Amit Lavon.
The new version introduces a few additions to what the godoc tool can
recognize. You can see them below at Links, Headings and Lists.
The official documentation is at: https://go.dev/doc/comment
This function is named Example(), which is a package level test
function.
This function is named Example_other(), which is a package level test
function and has the label "other".
A whole file example is a file that ends in `_test.go` and contains exactly one
example function, no test or benchmark functions, and at least one other
package-level declaration.
Output:
Example
¶
fmt.Println("Hello")
Example
(other)
¶
fmt.Println("Hello")
Example
(wholeFilePackage)
¶
package main
import "fmt"
var message = "Hello"
// A whole file example is a file that ends in `_test.go` and contains exactly one
// example function, no test or benchmark functions, and at least one other
// package-level declaration.
func main() {
fmt.Println(message)
}
Hello
FuncExampleWithoutType is a func with an associated example (but not associated type). See Examples regarding the details.
You can use words other than "BUG", like "TODO". General syntax: MARKER(uid), with MARKER at least 2 chars (all caps) and uid at least 1 char.
By default, only BUG notes are shown. If you run a godoc server locally, you can control that with the -notes argument. For example, -notes="BUG|TODO".
You can embed blocks of code in your godoc, such as this:
fmt.Println("Hello")
To do that, simply add an extra indent to your comment's text.
For example, the code of the first lines of this section looks like this:
// You can embed blocks of code in your godoc, such as this: // fmt.Println("Hello") // To do that, simply add an extra indent to your comment's text.
While there are no built in enums in go, you can use types and constants to make enum lookalikes (documentation-wise). Take this Enums type for example; if you have a const/var clause where all the values are of the same type, it will be attached to that type's godoc. See below.
You can place usage examples in your godoc.
Examples should be placed in a file with a _test suffix. For example, the examples in this guide are in a file called doc_test.go .
The example functions should be called Example() for package examples, ExampleTypename() for a specific type or ExampleFuncname() for a specific function, ExampleTypename_Methodname() for a method on a specific type.
For multiple examples for the same entity (like same function), you can add a suffix starting with a lowercase like ExampleFoo_suffix1, ExampleFoo_suffix2.
You can document an example's output, by adding an output comment at its end. The output comment must begin with "Output:", as shown below:
func ExampleExamples_output() { fmt.Println("Hello") // Output: Hello }
Notice that the tricks brought here (headings, code blocks, links etc.) don't work in example documentation.
For full documentation of examples, see:
http://golang.org/pkg/testing/
This function is named ExampleExamples(), this way godoc knows to associate
it with the Examples type.
This function is named ExampleExamples_other(), it is associated with
Examples type and has the label "Other".
This is how godoc parsed ExampleExamples_output() that was shown above.
A whole file example can also be attached to a given type (Examples here)
with appropriate naming.
A whole file example is a file that ends in `_test.go` and contains exactly one
example function, no test or benchmark functions, and at least one other
package-level declaration.
Example
¶
fmt.Println("Hello")
Example
(other)
¶
fmt.Println("Hello")
Example
(output)
¶
fmt.Println("Hello")
// Output: Hello
Example
(wholeFileType)
¶
package main
import "fmt"
var messageExample = "Hello"
// A whole file example can also be attached to a given type (Examples here)
// with appropriate naming.
//
// A whole file example is a file that ends in `_test.go` and contains exactly one
// example function, no test or benchmark functions, and at least one other
// package-level declaration.
func main() {
fmt.Println(messageExample)
}
NewExamples is a func with an associated example. See Examples regarding the details.
ExampleMethod is a method with an associated example. See Examples regarding the details.
Go code that you upload to public repositories on github appears automatically on the godoc website. Just like this tutorial! Just check in your code and watch as it appears. Use this page's URL as reference.
The godoc page gets updated once you make a new release.
You can make headings in your godoc. An explicit heading line starts with a '#' and is separated from the previous and next paragraphs with empty comment lines. An implicit heading is the same without the '#' (`go fmt` will automatically add a leading #). It needs to begin with a capital letter and not end with punctuation.
See the full documentation here: https://go.dev/doc/comment#headings
For example, this code:
// # Explicit Heading // // Yada yada. // // Implicit Heading // // Yolo yolo.
Results in:
Yada yada.
Yolo yolo.
type Interface interface { Method() }
Interface are rendered like Struct:
There are several ways to embed links in godoc.
Web addresses will automatically generate links in the HTML output, like this: http://www.golang.org
You can link to identifiers in the documentation with square brackets.
Lines of the form "[something]: URL" turn the occurrences of [something] into links to that URL with the text "something". Lines of that form need to be separated from regular text with empty doc lines.
For example:
You can search for Go [online]. See our [Links] section or [Methods.Foo]. Go to the imported [doc] package or the [doc.Note] struct. Visit us under [github.com/fluhus/godoc-tricks.Links] [online]: https://duckduckgo.com/?q=golang
Results in:
You can search for Go online. See our Links section or Methods.Foo. Go to the imported doc package or the doc.Note struct. Visit us under github.com/fluhus/godoc-tricks.Links.
Indented lines that start with dashes, stars, or pluses (or other unicode characters) create lists.
For example:
This is my list - item 1 - item 2
Results in:
This is my list
You can run a local godoc server. This is helpful for previewing documentation, or for cases where you don't have a stable internet connection.
go run golang.org/x/tools/cmd/godoc@latest
godoc will listen on http://localhost:6060. Run with -h to see how you can control the port number and other options.
Methods are functions with receivers. Godoc associates methods with their receivers and attaches their documentation. See below.
Functions that construct an instance of a type (or a pointer to it) are associated with the returned type.
Methods are attached to their receiver type in the godoc, regardless of their physical location in the code.
Pointer receivers are also associated in the same way.
To start a new paragraph, add an empty line in the comment between the 2 paragraphs.
For example:
// Paragraph 1. // Still paragraph 1. // // Paragraph 2. // Still Paragraph 2.
Results in:
Paragraph 1. Still paragraph 1.
Paragraph 2. Still Paragraph 2.
type Struct struct { // ExportedField can have a comment on the line above. ExportedField int OtherExportedField Links // or on the same line FieldWithoutComment struct{} // contains filtered or unexported fields }
Struct will be shown with their exported fields. Unexported fields will be hidden under a comment "contains filtered or unexported fields". For instance the following struct
type Struct struct { // ExportedField can have a comment on the line above. ExportedField int OtherExportedField Links // or on the same line FieldWithoutComment struct{} hidden int // unexported fields (and their comments) will not be shown in the documentation }
will be rendered as:
⚠ This is an example bug. See the [Bugs] section.
https://github.com/fluhus/godoc-tricks
git clone
https://github.com/fluhus/godoc-tricks.git git@github.com:fluhus/godoc-tricks.git