« Back to Index

Go: documentation generate example

View original Gist on GitHub

Tags: #go

Golang documentation generate example.md

Create a file in the same package as the code and give it the format example_<whatever>_test.go.

The file should use the external package reference, e.g. package <your_package>_test.

The function name should be in the format: Example<Type>_<Method> or just Example<Func> if it’s a global package function.

An official example is json.Marshal which can be found in the file src/encoding/json/example_test.go and uses the package json_test as it’s part of the json package and the function is named ExampleMarshal.

If you see examples like Decoder.Decode (Stream) this is generated by using more than one underscore: Example<Type>_<Method>_text_to_be_inside_parentheses (e.g. ExampleDecoder_Decode_stream).