Generating Documentation

Let’s learn about generating documentation.

This lesson discusses how to create documentation for our Go code using the code of the post05 package as an example. The new package is renamed and is now called document.

Using comments for documentation

Go follows a simple rule regarding documentation: in order to document a function, a method, a variable, or even the package itself, we can write comments. As usual, that should be located directly before the element we want to document, without any empty lines in between. We can use one or more single-line comments, which are lines beginning with //, or block comments, which begin with /* and end with */—everything in between is considered a comment.

Note: It is highly recommended that each Go package we create has a block comment preceding the package declaration that introduces developers to the package and also explains what the package does.

Coding example

Instead of presenting the entire code of the post05 package, we will only present the important part, which means that function implementations are going to be return statements only. The new version of post05.go is called document.go and comes with the following code and comments:

Get hands-on with 1200+ tech skills courses.