<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Paweł Słomka</title>
    <description>The latest articles on DEV Community by Paweł Słomka (@slomek).</description>
    <link>https://dev.to/slomek</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F56958%2Fd9eddd2a-9c69-4ff6-8d9f-ba6a65db0719.png</url>
      <title>DEV Community: Paweł Słomka</title>
      <link>https://dev.to/slomek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/slomek"/>
    <language>en</language>
    <item>
      <title>Testable Examples in Go</title>
      <dc:creator>Paweł Słomka</dc:creator>
      <pubDate>Wed, 07 Feb 2018 21:23:52 +0000</pubDate>
      <link>https://dev.to/slomek/testable-examples-in-go--1f0k</link>
      <guid>https://dev.to/slomek/testable-examples-in-go--1f0k</guid>
      <description>&lt;p&gt;This post has been originally published on my blog, &lt;a href="https://mycodesmells.com/post/testable-examples-in-go"&gt;MyCodeSmells.com&lt;/a&gt;. Check it out for more content!&lt;/p&gt;

&lt;p&gt;Writing technical documentation for the application is often more difficult, than creating its code. We, developers, know our craft very well, but when it comes to leaving some signs of how things work, how to use them, etc., we are surprisingly ineffective. On the other hand, when we are about to use some external dependency, we'd love to see examples of how it can be inserted into our code. How do we do that in Go?&lt;/p&gt;

&lt;p&gt;When I first heard about testable examples in Go, I didn't think it was such a great idea. &lt;/p&gt;

&lt;h2&gt;
  
  
  Enhancing documentation
&lt;/h2&gt;

&lt;p&gt;When writing an application in Go, we often do two things: write production code and tests. Those come naturally to anyone and is not specific to this programming language. Sometimes we add another piece, which is benchmarks because it takes very little effort to make them. There is one more thing you could do, but it's the least popular since they are great for the users of the package, not so much of the authors.&lt;/p&gt;

&lt;p&gt;You can think of examples as of the tests that check the output that is being printed to the standard output. You can also, obviously, create examples by explicitly calling &lt;code&gt;fmt.Print(..)&lt;/code&gt; on a result of some function. This is because the main reason of example functions are to present a snippet of the code and show what is expected to be the output.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example example
&lt;/h2&gt;

&lt;p&gt;Imagine that we have an exported function that sums two integers:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// sum.go
func Sum(a, b int) int {
    return a + b
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It might be hard to imagine, but there might be some users that don't know how to use it. To do that, we need to create Example function for &lt;code&gt;Sum&lt;/code&gt;, so that it appears in the documentation (via &lt;code&gt;godoc&lt;/code&gt;):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// sum_test.go
func ExampleSum() {
    res := Sum(1, 5)
    fmt.Printf(res)
    // Output:
    // 6
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We can also make small modifications of our examples and identify them by one-word suffix in the example function name:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// sum_test.go
func ExampleSum_negative() {
    Sum(-3, -9)
    //Output:
    // -12
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Pretty easy, right?&lt;/p&gt;

&lt;p&gt;We can also create examples for functions of structs so that they are placed in a correct place in &lt;code&gt;godoc&lt;/code&gt; output:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// user.go
type User struct {
    Name string
}

func (u User) Hi() {
    fmt.Printf("Hi, my name is %s!", u.Name)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It's important to name the example function using the struct name and the function we want to showcase:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// user_test.go
func ExampleUser_Hi() {
    u := User{"Timmy"}
    u.Hi()
    // Output:
    // Hi, my name is Timmy!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Running examples
&lt;/h2&gt;

&lt;p&gt;Once you create your example functions, you have to make sure that the &lt;code&gt;// Output:&lt;/code&gt; comment section actually show the output that is returned by the code written above. &lt;/p&gt;

&lt;p&gt;Running examples is very simple because they are executed via &lt;code&gt;go test&lt;/code&gt;, that is just as any &lt;em&gt;normal&lt;/em&gt; tests:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go test
--- FAIL: ExampleUser_Hi (0.00s)
got:
Hi, my name is not Timmy!
want:
Hi, my name is Timmy!
FAIL
exit status 1
FAIL    github.com/mycodesmells/golang-examples/misc/examples 0.005s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you can see, writing examples require very little effort, but they can make your documentation more complete, and make other people more eager to use your packages since they can see how your code works.&lt;/p&gt;

&lt;p&gt;The source code of these examples are available &lt;a href="https://github.com/mycodesmells/golang-examples/tree/master/misc/examples"&gt;on Github&lt;/a&gt;, and the documentation is available &lt;a href="https://godoc.org/github.com/mycodesmells/golang-examples/misc/examples"&gt;on GoDoc&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>go</category>
      <category>documentation</category>
      <category>tests</category>
    </item>
  </channel>
</rss>
