Allow documentation examples with any function signature
Today an example function must take no arguments and return nothing. That makes it awkward to show code that returns an error or needs a *testing.T. This proposal lets example functions (Example or ExampleXxx, with the same naming rules as today) have any parameters and results.
func Example_returningAnError() error {
f, err := os.Open("testfile")
if err != nil {
return err
}
defer f.Close()
examplepackage.F(f)
return nil
}
func TestExample_returningAnError(t *testing.T) {
if err := Example_returningAnError(); err != nil {
t.Fatal(err)
}
}
Go doc shows such examples in the usual place, as the complete function including its doc comment. They are not runnable in the playground, and go test does not run them.
- Proposal
- #79808 by neild
- Status
- Accepted on Jul 8, 2026
- Impact
- medium
- Area
- testing