typestar

Examples are tests in Go

An Example function with an Output comment is compiled, run and checked.

func Stars(tpm int) string {
    if tpm >= 100 {
        return "***"
    }
    return "*.."
}

func ExampleStars() {
    fmt.Println(Stars(104))
    fmt.Println(Stars(80))
    // Output:
    // ***
    // *..
}

func ExampleStars_slow() {
    fmt.Println(Stars(10))
    // Output: *..
}

How it works

  1. The name links it to what it documents: ExampleStars.
  2. The Output comment must match exactly.
  3. It appears in the generated documentation.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
239
Tokens
62
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 34 seconds.

Type this snippet

Step 3 of 3 in Measuring & generating, step 10 of 13 in Testing.

← Previous Next →