Writing Documentation for Golang

back

To use go doc, it’s needed to include comments in the code that are formatted in a specific way. These comments should be placed immediately above the code that they describe.

go mod init add
// Package math provides basic math operations.
// It has only one file:
//   - add.go
package add

// Add adds integers and returns the result.
//
// Example:
//
//  sum := add.Add(1, 2)
//  fmt.Println(sum) // Output: 3
//  sum := add.Add(1, 2, 3)
//  fmt.Println(sum) // Output: 6
func Add(a ...int) int {
  total := 0
  for i := range a {
    total += a[i]
  }
  return total
}
go doc add
package add // import "add"

Package math provides basic math operations. It has only one file:
  - add.go

func Add(a ...int) int
go doc add.Add
package add // import "add"

func Add(a ...int) int
    Add adds integers and returns the result.

    Example:

        sum := add.Add(1, 2)
        fmt.Println(sum) // Output: 3
        sum := add.Add(1, 2, 3)
        fmt.Println(sum) // Output: 6
# as of 1.18, it should be installed via:
# `go get` in 1.18 and beyond will no longer install executables.
go get golang.org/x/tools/cmd/godoc
go install golang.org/x/tools/cmd/godoc

godoc -http=:6060
open http://localhost:6060

image