DEV Community

Discussion on: Do you put examples/ directory with sample code for integrating your library?

Collapse
 
quii profile image
Chris James

This is one of the really nice things about Go.

It has built in support for examples in the testing framework. blog.golang.org/examples

So you can put stuff like this in your code

func ExampleReverse() {
    fmt.Println(stringutil.Reverse("hello"))
    // Output: olleh
}

And they will automatically appear in the documentation.

Example

The advantage of this over textual documentation (which often diverges from reality) is it is checked to be syntactically correct by the compiler and checked that it actually does what it says it does by running it as a test.

Collapse
 
adhocore profile image
Jitendra

that is pretty awesome :)

Collapse
 
david_j_eddy profile image
David J Eddy

Golang is a pretty awesome language TBH. The more I use it, the more I like it. So much of it just makes sense. You can defiantly see the lessons the language authors learned from the past and applied forward.