mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
godoc: make example code more readable with new comment convention
go/doc: move Examples to go/ast
cmd/go: use go/doc to read examples
src/pkg: update examples to use new convention
This is to make whole file examples more readable. When presented as a
complete function, preceding an Example with its output is confusing.
The new convention is to put the expected output in the final comment
of the example, preceded by the string "output:" (case insensitive).
An idiomatic example looks like this:
// This example demonstrates Foo by doing bar and quux.
func ExampleFoo() {
// example body that does bar and quux
// Output:
// example output
}
R=rsc, gri
CC=golang-dev
https://golang.org/cl/5673053
This commit is contained in:
parent
fa0100cf26
commit
11e113db57
14 changed files with 213 additions and 148 deletions
|
|
@ -38,16 +38,25 @@
|
|||
// }
|
||||
// }
|
||||
//
|
||||
// The package also runs and verifies example code. Example functions
|
||||
// include an introductory comment that is compared with the standard output
|
||||
// of the function when the tests are run, as in this example of an example:
|
||||
// The package also runs and verifies example code. Example functions may
|
||||
// include a concluding comment that begins with "Output:" and is compared with
|
||||
// the standard output of the function when the tests are run, as in these
|
||||
// examples of an example:
|
||||
//
|
||||
// // hello
|
||||
// func ExampleHello() {
|
||||
// fmt.Println("hello")
|
||||
// // Output: hello
|
||||
// }
|
||||
//
|
||||
// Example functions without comments are compiled but not executed.
|
||||
// func ExampleSalutations() {
|
||||
// fmt.Println("hello, and")
|
||||
// fmt.Println("goodbye")
|
||||
// // Output:
|
||||
// // hello, and
|
||||
// // goodbye
|
||||
// }
|
||||
//
|
||||
// Example functions without output comments are compiled but not executed.
|
||||
//
|
||||
// The naming convention to declare examples for a function F, a type T and
|
||||
// method M on type T are:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue