[go: up one dir, main page]

Today I Learned

tags


2025/01/30

That you can write golang examples that get run using go test:

func ExampleHello() {
   fmt.Println("hello")
   // Output: hello
}

The naming convention to declare examples for the package, a function F, a type T and method M on type T are:

func Example() { ... }
func ExampleF() { ... }
func ExampleT() { ... }
func ExampleT_M() { ... }

https://pkg.go.dev/testing#hdr-Examples

I feel like this might be able to be combined with doc comments.