phemmer
phemmer

Reputation: 8797

Go example won't run

I'm trying to add an example to a package, and run the example via go test, however the example is never run.

For example, see this gist: https://gist.github.com/85469ecc65bb5bb85857

The gist has example_test.go:

package cow_test

import (
    cow "gist.github.com/85469ecc65bb5bb85857"
)

func Example() {
    cow.Poke()
}

Yet when I run this:

# go test -v example_test.go 
testing: warning: no tests to run
PASS
ok      command-line-arguments  0.002s

However other packages from stdlib work just fine:

# cd /usr/lib/go/src/errors
# go test -v example_test.go 
=== RUN: Example
--- PASS: Example (0.00s)
PASS
ok      command-line-arguments  0.002s

What is wrong with my example?

Upvotes: 16

Views: 10073

Answers (1)

Thundercat
Thundercat

Reputation: 120931

From the documentation:

Example functions without output comments are compiled but not executed.

Add an output comment:

func Example() {
    junk.Poke()
    // Output: MOOOO!
}

Upvotes: 32

Related Questions