- In Go, comments play an important role in documenting code.
- They are used by the godoc command, which extracts these comments to create documentation about Go packages.
- Package comments should be written directly before a package clause (
package x
) and begin withPackage x ...
like this.
// Package kelvin provides tools to convert temperatures to and from Kelvin.
package kelvin
- A function comment should be written directly before the function declaration.
- It should also explain what arguments the function takes, what it does with them, and what its return values mean, ending in a period):
// CelsiusFreezingTemp returns an integer value equal to the temperature at which water freezes in degrees Celsius.
func CelsiusFreezingTemp() int {
return 0
}
- Golint is a great tool to check for missing comments and other common stylistic issues, which you can read more about at Effective Go.