diff options
Diffstat (limited to 'src/go/doc/testdata/examples')
25 files changed, 1044 insertions, 0 deletions
diff --git a/src/go/doc/testdata/examples/README.md b/src/go/doc/testdata/examples/README.md new file mode 100644 index 0000000..a1c18e8 --- /dev/null +++ b/src/go/doc/testdata/examples/README.md @@ -0,0 +1,12 @@ +These files are processed by example_test.go:TestExamples. + +A .golden file is a txtar file with two sections for each example that should be +created by doc.Examples from the corresponding .go file. + +One section, named EXAMPLE_NAME.Output, contains the example's output, +the value of the field Example.Output. + +The other, named EXAMPLE_NAME.Play, contains the formatted code for a playable +version of the example, the value of the field Example.Play. + +If a section is missing, it is treated as being empty. diff --git a/src/go/doc/testdata/examples/empty.go b/src/go/doc/testdata/examples/empty.go new file mode 100644 index 0000000..0b10420 --- /dev/null +++ b/src/go/doc/testdata/examples/empty.go @@ -0,0 +1,8 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package p + +func Example() {} +func Example_a() diff --git a/src/go/doc/testdata/examples/empty.golden b/src/go/doc/testdata/examples/empty.golden new file mode 100644 index 0000000..2aafd20 --- /dev/null +++ b/src/go/doc/testdata/examples/empty.golden @@ -0,0 +1,6 @@ +-- .Play -- +package main + +func main() {} +func main() + diff --git a/src/go/doc/testdata/examples/generic_constraints.go b/src/go/doc/testdata/examples/generic_constraints.go new file mode 100644 index 0000000..ea5d2b3 --- /dev/null +++ b/src/go/doc/testdata/examples/generic_constraints.go @@ -0,0 +1,38 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package p_test + +import ( + "fmt" + "time" +) + +type C1 interface { + string | int +} + +type C2 interface { + M(time.Time) +} + +type G[T C1] int + +func g[T C2](x T) {} + +type Tm int + +func (Tm) M(time.Time) {} + +type Foo int + +func Example() { + fmt.Println("hello") +} + +func ExampleGeneric() { + var x G[string] + g(Tm(3)) + fmt.Println(x) +} diff --git a/src/go/doc/testdata/examples/generic_constraints.golden b/src/go/doc/testdata/examples/generic_constraints.golden new file mode 100644 index 0000000..6c7b0ed --- /dev/null +++ b/src/go/doc/testdata/examples/generic_constraints.golden @@ -0,0 +1,39 @@ +-- .Play -- +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("hello") +} +-- Generic.Play -- +package main + +import ( + "fmt" + "time" +) + +type C1 interface { + string | int +} + +type C2 interface { + M(time.Time) +} + +type G[T C1] int + +func g[T C2](x T) {} + +type Tm int + +func (Tm) M(time.Time) {} + +func main() { + var x G[string] + g(Tm(3)) + fmt.Println(x) +} diff --git a/src/go/doc/testdata/examples/import_groups.go b/src/go/doc/testdata/examples/import_groups.go new file mode 100644 index 0000000..05f21ca --- /dev/null +++ b/src/go/doc/testdata/examples/import_groups.go @@ -0,0 +1,23 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package foo_test + +import ( + "fmt" + "time" + + "golang.org/x/time/rate" +) + +func Example() { + fmt.Println("Hello, world!") + // Output: Hello, world! +} + +func ExampleLimiter() { + // Uses fmt, time and rate. + l := rate.NewLimiter(rate.Every(time.Second), 1) + fmt.Println(l) +} diff --git a/src/go/doc/testdata/examples/import_groups.golden b/src/go/doc/testdata/examples/import_groups.golden new file mode 100644 index 0000000..efe2cc1 --- /dev/null +++ b/src/go/doc/testdata/examples/import_groups.golden @@ -0,0 +1,27 @@ +-- .Play -- +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello, world!") +} +-- .Output -- +Hello, world! +-- Limiter.Play -- +package main + +import ( + "fmt" + "time" + + "golang.org/x/time/rate" +) + +func main() { + // Uses fmt, time and rate. + l := rate.NewLimiter(rate.Every(time.Second), 1) + fmt.Println(l) +} diff --git a/src/go/doc/testdata/examples/import_groups_named.go b/src/go/doc/testdata/examples/import_groups_named.go new file mode 100644 index 0000000..377022b --- /dev/null +++ b/src/go/doc/testdata/examples/import_groups_named.go @@ -0,0 +1,23 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package foo_test + +import ( + "fmt" + tm "time" + + r "golang.org/x/time/rate" +) + +func Example() { + fmt.Println("Hello, world!") + // Output: Hello, world! +} + +func ExampleLimiter() { + // Uses fmt, time and rate. + l := r.NewLimiter(r.Every(tm.Second), 1) + fmt.Println(l) +} diff --git a/src/go/doc/testdata/examples/import_groups_named.golden b/src/go/doc/testdata/examples/import_groups_named.golden new file mode 100644 index 0000000..9baf373 --- /dev/null +++ b/src/go/doc/testdata/examples/import_groups_named.golden @@ -0,0 +1,27 @@ +-- .Play -- +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello, world!") +} +-- .Output -- +Hello, world! +-- Limiter.Play -- +package main + +import ( + "fmt" + tm "time" + + r "golang.org/x/time/rate" +) + +func main() { + // Uses fmt, time and rate. + l := r.NewLimiter(r.Every(tm.Second), 1) + fmt.Println(l) +} diff --git a/src/go/doc/testdata/examples/inspect_signature.go b/src/go/doc/testdata/examples/inspect_signature.go new file mode 100644 index 0000000..c4a36e7 --- /dev/null +++ b/src/go/doc/testdata/examples/inspect_signature.go @@ -0,0 +1,23 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package foo_test + +import ( + "bytes" + "io" +) + +func getReader() io.Reader { return nil } + +func do(b bytes.Reader) {} + +func Example() { + getReader() + do() + // Output: +} + +func ExampleIgnored() { +} diff --git a/src/go/doc/testdata/examples/inspect_signature.golden b/src/go/doc/testdata/examples/inspect_signature.golden new file mode 100644 index 0000000..c0d9b2e --- /dev/null +++ b/src/go/doc/testdata/examples/inspect_signature.golden @@ -0,0 +1,24 @@ +-- .Play -- +package main + +import ( + "bytes" + "io" +) + +func getReader() io.Reader { return nil } + +func do(b bytes.Reader) {} + +func main() { + getReader() + do() +} +-- Ignored.Play -- +package main + +import () + +func main() { +} + diff --git a/src/go/doc/testdata/examples/iota.go b/src/go/doc/testdata/examples/iota.go new file mode 100644 index 0000000..c878b77 --- /dev/null +++ b/src/go/doc/testdata/examples/iota.go @@ -0,0 +1,34 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package foo_test + +const ( + a = iota + b +) + +const ( + c = 3 + d = 4 +) + +const ( + e = iota + f +) + +// The example refers to only one of the constants in the iota group, but we +// must keep all of them because of the iota. The second group of constants can +// be trimmed. The third has an iota, but is unused, so it can be eliminated. + +func Example() { + _ = b + _ = d +} + +// Need two examples to hit the playExample function. + +func Example2() { +} diff --git a/src/go/doc/testdata/examples/iota.golden b/src/go/doc/testdata/examples/iota.golden new file mode 100644 index 0000000..7487702 --- /dev/null +++ b/src/go/doc/testdata/examples/iota.golden @@ -0,0 +1,23 @@ +-- .Play -- +package main + +import () + +const ( + a = iota + b +) + +const d = 4 + +func main() { + _ = b + _ = d +} +-- 2.Play -- +package main + +import () + +func main() { +} diff --git a/src/go/doc/testdata/examples/issue43658.go b/src/go/doc/testdata/examples/issue43658.go new file mode 100644 index 0000000..385223a --- /dev/null +++ b/src/go/doc/testdata/examples/issue43658.go @@ -0,0 +1,223 @@ +// Copyright ©2016 The Gonum Authors. All rights reserved. +// Copyright 2021 The Go Authors. All rights reserved. +// (above line required for our license-header checker) +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package community_test + +import ( + "fmt" + "log" + "sort" + + "golang.org/x/exp/rand" + + "gonum.org/v1/gonum/graph/community" + "gonum.org/v1/gonum/graph/internal/ordered" + "gonum.org/v1/gonum/graph/simple" +) + +func ExampleProfile_simple() { + // Profile calls Modularize which implements the Louvain modularization algorithm. + // Since this is a randomized algorithm we use a defined random source to ensure + // consistency between test runs. In practice, results will not differ greatly + // between runs with different PRNG seeds. + src := rand.NewSource(1) + + // Create dumbell graph: + // + // 0 4 + // |\ /| + // | 2 - 3 | + // |/ \| + // 1 5 + // + g := simple.NewUndirectedGraph() + for u, e := range smallDumbell { + for v := range e { + g.SetEdge(simple.Edge{F: simple.Node(u), T: simple.Node(v)}) + } + } + + // Get the profile of internal node weight for resolutions + // between 0.1 and 10 using logarithmic bisection. + p, err := community.Profile( + community.ModularScore(g, community.Weight, 10, src), + true, 1e-3, 0.1, 10, + ) + if err != nil { + log.Fatal(err) + } + + // Print out each step with communities ordered. + for _, d := range p { + comm := d.Communities() + for _, c := range comm { + sort.Sort(ordered.ByID(c)) + } + sort.Sort(ordered.BySliceIDs(comm)) + fmt.Printf("Low:%.2v High:%.2v Score:%v Communities:%v Q=%.3v\n", + d.Low, d.High, d.Score, comm, community.Q(g, comm, d.Low)) + } + + // Output: + // Low:0.1 High:0.29 Score:14 Communities:[[0 1 2 3 4 5]] Q=0.9 + // Low:0.29 High:2.3 Score:12 Communities:[[0 1 2] [3 4 5]] Q=0.714 + // Low:2.3 High:3.5 Score:4 Communities:[[0 1] [2] [3] [4 5]] Q=-0.31 + // Low:3.5 High:10 Score:0 Communities:[[0] [1] [2] [3] [4] [5]] Q=-0.607 +} + +// intset is an integer set. +type intset map[int]struct{} + +func linksTo(i ...int) intset { + if len(i) == 0 { + return nil + } + s := make(intset) + for _, v := range i { + s[v] = struct{}{} + } + return s +} + +var ( + smallDumbell = []intset{ + 0: linksTo(1, 2), + 1: linksTo(2), + 2: linksTo(3), + 3: linksTo(4, 5), + 4: linksTo(5), + 5: nil, + } + + // http://www.slate.com/blogs/the_world_/2014/07/17/the_middle_east_friendship_chart.html + middleEast = struct{ friends, complicated, enemies []intset }{ + // green cells + friends: []intset{ + 0: nil, + 1: linksTo(5, 7, 9, 12), + 2: linksTo(11), + 3: linksTo(4, 5, 10), + 4: linksTo(3, 5, 10), + 5: linksTo(1, 3, 4, 8, 10, 12), + 6: nil, + 7: linksTo(1, 12), + 8: linksTo(5, 9, 11), + 9: linksTo(1, 8, 12), + 10: linksTo(3, 4, 5), + 11: linksTo(2, 8), + 12: linksTo(1, 5, 7, 9), + }, + + // yellow cells + complicated: []intset{ + 0: linksTo(2, 4), + 1: linksTo(4, 8), + 2: linksTo(0, 3, 4, 5, 8, 9), + 3: linksTo(2, 8, 11), + 4: linksTo(0, 1, 2, 8), + 5: linksTo(2), + 6: nil, + 7: linksTo(9, 11), + 8: linksTo(1, 2, 3, 4, 10, 12), + 9: linksTo(2, 7, 11), + 10: linksTo(8), + 11: linksTo(3, 7, 9, 12), + 12: linksTo(8, 11), + }, + + // red cells + enemies: []intset{ + 0: linksTo(1, 3, 5, 6, 7, 8, 9, 10, 11, 12), + 1: linksTo(0, 2, 3, 6, 10, 11), + 2: linksTo(1, 6, 7, 10, 12), + 3: linksTo(0, 1, 6, 7, 9, 12), + 4: linksTo(6, 7, 9, 11, 12), + 5: linksTo(0, 6, 7, 9, 11), + 6: linksTo(0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12), + 7: linksTo(0, 2, 3, 4, 5, 6, 8, 10), + 8: linksTo(0, 6, 7), + 9: linksTo(0, 3, 4, 5, 6, 10), + 10: linksTo(0, 1, 2, 6, 7, 9, 11, 12), + 11: linksTo(0, 1, 4, 5, 6, 10), + 12: linksTo(0, 2, 3, 4, 6, 10), + }, + } +) + +var friends, enemies *simple.WeightedUndirectedGraph + +func init() { + friends = simple.NewWeightedUndirectedGraph(0, 0) + for u, e := range middleEast.friends { + // Ensure unconnected nodes are included. + if friends.Node(int64(u)) == nil { + friends.AddNode(simple.Node(u)) + } + for v := range e { + friends.SetWeightedEdge(simple.WeightedEdge{F: simple.Node(u), T: simple.Node(v), W: 1}) + } + } + enemies = simple.NewWeightedUndirectedGraph(0, 0) + for u, e := range middleEast.enemies { + // Ensure unconnected nodes are included. + if enemies.Node(int64(u)) == nil { + enemies.AddNode(simple.Node(u)) + } + for v := range e { + enemies.SetWeightedEdge(simple.WeightedEdge{F: simple.Node(u), T: simple.Node(v), W: -1}) + } + } +} + +func ExampleProfile_multiplex() { + // Profile calls ModularizeMultiplex which implements the Louvain modularization + // algorithm. Since this is a randomized algorithm we use a defined random source + // to ensure consistency between test runs. In practice, results will not differ + // greatly between runs with different PRNG seeds. + src := rand.NewSource(1) + + // The undirected graphs, friends and enemies, are the political relationships + // in the Middle East as described in the Slate article: + // http://www.slate.com/blogs/the_world_/2014/07/17/the_middle_east_friendship_chart.html + g, err := community.NewUndirectedLayers(friends, enemies) + if err != nil { + log.Fatal(err) + } + weights := []float64{1, -1} + + // Get the profile of internal node weight for resolutions + // between 0.1 and 10 using logarithmic bisection. + p, err := community.Profile( + community.ModularMultiplexScore(g, weights, true, community.WeightMultiplex, 10, src), + true, 1e-3, 0.1, 10, + ) + if err != nil { + log.Fatal(err) + } + + // Print out each step with communities ordered. + for _, d := range p { + comm := d.Communities() + for _, c := range comm { + sort.Sort(ordered.ByID(c)) + } + sort.Sort(ordered.BySliceIDs(comm)) + fmt.Printf("Low:%.2v High:%.2v Score:%v Communities:%v Q=%.3v\n", + d.Low, d.High, d.Score, comm, community.QMultiplex(g, comm, weights, []float64{d.Low})) + } + + // Output: + // Low:0.1 High:0.72 Score:26 Communities:[[0] [1 7 9 12] [2 8 11] [3 4 5 10] [6]] Q=[24.7 1.97] + // Low:0.72 High:1.1 Score:24 Communities:[[0 6] [1 7 9 12] [2 8 11] [3 4 5 10]] Q=[16.9 14.1] + // Low:1.1 High:1.2 Score:18 Communities:[[0 2 6 11] [1 7 9 12] [3 4 5 8 10]] Q=[9.16 25.1] + // Low:1.2 High:1.6 Score:10 Communities:[[0 3 4 5 6 10] [1 7 9 12] [2 8 11]] Q=[10.5 26.7] + // Low:1.6 High:1.6 Score:8 Communities:[[0 1 6 7 9 12] [2 8 11] [3 4 5 10]] Q=[5.56 39.8] + // Low:1.6 High:1.8 Score:2 Communities:[[0 2 3 4 5 6 10] [1 7 8 9 11 12]] Q=[-1.82 48.6] + // Low:1.8 High:2.3 Score:-6 Communities:[[0 2 3 4 5 6 8 10 11] [1 7 9 12]] Q=[-5 57.5] + // Low:2.3 High:2.4 Score:-10 Communities:[[0 1 2 6 7 8 9 11 12] [3 4 5 10]] Q=[-11.2 79] + // Low:2.4 High:4.3 Score:-52 Communities:[[0 1 2 3 4 5 6 7 8 9 10 11 12]] Q=[-46.1 117] + // Low:4.3 High:10 Score:-54 Communities:[[0 1 2 3 4 6 7 8 9 10 11 12] [5]] Q=[-82 254] +} diff --git a/src/go/doc/testdata/examples/issue43658.golden b/src/go/doc/testdata/examples/issue43658.golden new file mode 100644 index 0000000..5200d14 --- /dev/null +++ b/src/go/doc/testdata/examples/issue43658.golden @@ -0,0 +1,156 @@ +-- Profile_simple.Play -- +package main + +import ( + "fmt" + "log" + "sort" + + "golang.org/x/exp/rand" + + "gonum.org/v1/gonum/graph/community" + "gonum.org/v1/gonum/graph/internal/ordered" + "gonum.org/v1/gonum/graph/simple" +) + +func main() { + // Profile calls Modularize which implements the Louvain modularization algorithm. + // Since this is a randomized algorithm we use a defined random source to ensure + // consistency between test runs. In practice, results will not differ greatly + // between runs with different PRNG seeds. + src := rand.NewSource(1) + + // Create dumbell graph: + // + // 0 4 + // |\ /| + // | 2 - 3 | + // |/ \| + // 1 5 + // + g := simple.NewUndirectedGraph() + for u, e := range smallDumbell { + for v := range e { + g.SetEdge(simple.Edge{F: simple.Node(u), T: simple.Node(v)}) + } + } + + // Get the profile of internal node weight for resolutions + // between 0.1 and 10 using logarithmic bisection. + p, err := community.Profile( + community.ModularScore(g, community.Weight, 10, src), + true, 1e-3, 0.1, 10, + ) + if err != nil { + log.Fatal(err) + } + + // Print out each step with communities ordered. + for _, d := range p { + comm := d.Communities() + for _, c := range comm { + sort.Sort(ordered.ByID(c)) + } + sort.Sort(ordered.BySliceIDs(comm)) + fmt.Printf("Low:%.2v High:%.2v Score:%v Communities:%v Q=%.3v\n", + d.Low, d.High, d.Score, comm, community.Q(g, comm, d.Low)) + } + +} + +// intset is an integer set. +type intset map[int]struct{} + +func linksTo(i ...int) intset { + if len(i) == 0 { + return nil + } + s := make(intset) + for _, v := range i { + s[v] = struct{}{} + } + return s +} + +var smallDumbell = []intset{ + 0: linksTo(1, 2), + 1: linksTo(2), + 2: linksTo(3), + 3: linksTo(4, 5), + 4: linksTo(5), + 5: nil, +} + +-- Profile_simple.Output -- +Low:0.1 High:0.29 Score:14 Communities:[[0 1 2 3 4 5]] Q=0.9 +Low:0.29 High:2.3 Score:12 Communities:[[0 1 2] [3 4 5]] Q=0.714 +Low:2.3 High:3.5 Score:4 Communities:[[0 1] [2] [3] [4 5]] Q=-0.31 +Low:3.5 High:10 Score:0 Communities:[[0] [1] [2] [3] [4] [5]] Q=-0.607 + +-- Profile_multiplex.Play -- + +package main + +import ( + "fmt" + "log" + "sort" + + "golang.org/x/exp/rand" + + "gonum.org/v1/gonum/graph/community" + "gonum.org/v1/gonum/graph/internal/ordered" + "gonum.org/v1/gonum/graph/simple" +) + +var friends, enemies *simple.WeightedUndirectedGraph + +func main() { + // Profile calls ModularizeMultiplex which implements the Louvain modularization + // algorithm. Since this is a randomized algorithm we use a defined random source + // to ensure consistency between test runs. In practice, results will not differ + // greatly between runs with different PRNG seeds. + src := rand.NewSource(1) + + // The undirected graphs, friends and enemies, are the political relationships + // in the Middle East as described in the Slate article: + // http://www.slate.com/blogs/the_world_/2014/07/17/the_middle_east_friendship_chart.html + g, err := community.NewUndirectedLayers(friends, enemies) + if err != nil { + log.Fatal(err) + } + weights := []float64{1, -1} + + // Get the profile of internal node weight for resolutions + // between 0.1 and 10 using logarithmic bisection. + p, err := community.Profile( + community.ModularMultiplexScore(g, weights, true, community.WeightMultiplex, 10, src), + true, 1e-3, 0.1, 10, + ) + if err != nil { + log.Fatal(err) + } + + // Print out each step with communities ordered. + for _, d := range p { + comm := d.Communities() + for _, c := range comm { + sort.Sort(ordered.ByID(c)) + } + sort.Sort(ordered.BySliceIDs(comm)) + fmt.Printf("Low:%.2v High:%.2v Score:%v Communities:%v Q=%.3v\n", + d.Low, d.High, d.Score, comm, community.QMultiplex(g, comm, weights, []float64{d.Low})) + } + +} +-- Profile_multiplex.Output -- +Low:0.1 High:0.72 Score:26 Communities:[[0] [1 7 9 12] [2 8 11] [3 4 5 10] [6]] Q=[24.7 1.97] +Low:0.72 High:1.1 Score:24 Communities:[[0 6] [1 7 9 12] [2 8 11] [3 4 5 10]] Q=[16.9 14.1] +Low:1.1 High:1.2 Score:18 Communities:[[0 2 6 11] [1 7 9 12] [3 4 5 8 10]] Q=[9.16 25.1] +Low:1.2 High:1.6 Score:10 Communities:[[0 3 4 5 6 10] [1 7 9 12] [2 8 11]] Q=[10.5 26.7] +Low:1.6 High:1.6 Score:8 Communities:[[0 1 6 7 9 12] [2 8 11] [3 4 5 10]] Q=[5.56 39.8] +Low:1.6 High:1.8 Score:2 Communities:[[0 2 3 4 5 6 10] [1 7 8 9 11 12]] Q=[-1.82 48.6] +Low:1.8 High:2.3 Score:-6 Communities:[[0 2 3 4 5 6 8 10 11] [1 7 9 12]] Q=[-5 57.5] +Low:2.3 High:2.4 Score:-10 Communities:[[0 1 2 6 7 8 9 11 12] [3 4 5 10]] Q=[-11.2 79] +Low:2.4 High:4.3 Score:-52 Communities:[[0 1 2 3 4 5 6 7 8 9 10 11 12]] Q=[-46.1 117] +Low:4.3 High:10 Score:-54 Communities:[[0 1 2 3 4 6 7 8 9 10 11 12] [5]] Q=[-82 254] diff --git a/src/go/doc/testdata/examples/multiple.go b/src/go/doc/testdata/examples/multiple.go new file mode 100644 index 0000000..2728264 --- /dev/null +++ b/src/go/doc/testdata/examples/multiple.go @@ -0,0 +1,98 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package foo_test + +import ( + "flag" + "fmt" + "log" + "os/exec" + "sort" +) + +func ExampleHello() { + fmt.Println("Hello, world!") + // Output: Hello, world! +} + +func ExampleImport() { + out, err := exec.Command("date").Output() + if err != nil { + log.Fatal(err) + } + fmt.Printf("The date is %s\n", out) +} + +func ExampleKeyValue() { + v := struct { + a string + b int + }{ + a: "A", + b: 1, + } + fmt.Print(v) + // Output: a: "A", b: 1 +} + +func ExampleKeyValueImport() { + f := flag.Flag{ + Name: "play", + } + fmt.Print(f) + // Output: Name: "play" +} + +var keyValueTopDecl = struct { + a string + b int +}{ + a: "B", + b: 2, +} + +func ExampleKeyValueTopDecl() { + fmt.Print(keyValueTopDecl) + // Output: a: "B", b: 2 +} + +// Person represents a person by name and age. +type Person struct { + Name string + Age int +} + +// String returns a string representation of the Person. +func (p Person) String() string { + return fmt.Sprintf("%s: %d", p.Name, p.Age) +} + +// ByAge implements sort.Interface for []Person based on +// the Age field. +type ByAge []Person + +// Len returns the number of elements in ByAge. +func (a ByAge) Len() int { return len(a) } + +// Swap swaps the elements in ByAge. +func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age } + +// people is the array of Person +var people = []Person{ + {"Bob", 31}, + {"John", 42}, + {"Michael", 17}, + {"Jenny", 26}, +} + +func ExampleSort() { + fmt.Println(people) + sort.Sort(ByAge(people)) + fmt.Println(people) + // Output: + // [Bob: 31 John: 42 Michael: 17 Jenny: 26] + // [Michael: 17 Jenny: 26 Bob: 31 John: 42] +} diff --git a/src/go/doc/testdata/examples/multiple.golden b/src/go/doc/testdata/examples/multiple.golden new file mode 100644 index 0000000..d2d791e --- /dev/null +++ b/src/go/doc/testdata/examples/multiple.golden @@ -0,0 +1,129 @@ +-- Hello.Play -- +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello, world!") +} +-- Hello.Output -- +Hello, world! +-- Import.Play -- +package main + +import ( + "fmt" + "log" + "os/exec" +) + +func main() { + out, err := exec.Command("date").Output() + if err != nil { + log.Fatal(err) + } + fmt.Printf("The date is %s\n", out) +} +-- KeyValue.Play -- +package main + +import ( + "fmt" +) + +func main() { + v := struct { + a string + b int + }{ + a: "A", + b: 1, + } + fmt.Print(v) +} +-- KeyValue.Output -- +a: "A", b: 1 +-- KeyValueImport.Play -- +package main + +import ( + "flag" + "fmt" +) + +func main() { + f := flag.Flag{ + Name: "play", + } + fmt.Print(f) +} +-- KeyValueImport.Output -- +Name: "play" +-- KeyValueTopDecl.Play -- +package main + +import ( + "fmt" +) + +var keyValueTopDecl = struct { + a string + b int +}{ + a: "B", + b: 2, +} + +func main() { + fmt.Print(keyValueTopDecl) +} +-- KeyValueTopDecl.Output -- +a: "B", b: 2 +-- Sort.Play -- +package main + +import ( + "fmt" + "sort" +) + +// Person represents a person by name and age. +type Person struct { + Name string + Age int +} + +// String returns a string representation of the Person. +func (p Person) String() string { + return fmt.Sprintf("%s: %d", p.Name, p.Age) +} + +// ByAge implements sort.Interface for []Person based on +// the Age field. +type ByAge []Person + +// Len returns the number of elements in ByAge. +func (a ByAge) Len() int { return len(a) } + +// Swap swaps the elements in ByAge. +func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age } + +// people is the array of Person +var people = []Person{ + {"Bob", 31}, + {"John", 42}, + {"Michael", 17}, + {"Jenny", 26}, +} + +func main() { + fmt.Println(people) + sort.Sort(ByAge(people)) + fmt.Println(people) +} +-- Sort.Output -- +[Bob: 31 John: 42 Michael: 17 Jenny: 26] +[Michael: 17 Jenny: 26 Bob: 31 John: 42] diff --git a/src/go/doc/testdata/examples/values.go b/src/go/doc/testdata/examples/values.go new file mode 100644 index 0000000..64b0de4 --- /dev/null +++ b/src/go/doc/testdata/examples/values.go @@ -0,0 +1,22 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package foo_test + +// Variable declaration with fewer values than names. + +func f() (int, int) { + return 1, 2 +} + +var a, b = f() + +// Need two examples to hit playExample. + +func ExampleA() { + _ = a +} + +func ExampleB() { +} diff --git a/src/go/doc/testdata/examples/values.golden b/src/go/doc/testdata/examples/values.golden new file mode 100644 index 0000000..00c1991 --- /dev/null +++ b/src/go/doc/testdata/examples/values.golden @@ -0,0 +1,21 @@ +-- A.Play -- +package main + +import () + +func f() (int, int) { + return 1, 2 +} + +var a, b = f() + +func main() { + _ = a +} +-- B.Play -- +package main + +import () + +func main() { +} diff --git a/src/go/doc/testdata/examples/whole_file.go b/src/go/doc/testdata/examples/whole_file.go new file mode 100644 index 0000000..61954ce --- /dev/null +++ b/src/go/doc/testdata/examples/whole_file.go @@ -0,0 +1,23 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package foo_test + +import "fmt" + +type X int + +func (X) Foo() { +} + +func (X) TestBlah() { +} + +func (X) BenchmarkFoo() { +} + +func Example() { + fmt.Println("Hello, world!") + // Output: Hello, world! +} diff --git a/src/go/doc/testdata/examples/whole_file.golden b/src/go/doc/testdata/examples/whole_file.golden new file mode 100644 index 0000000..74a2291 --- /dev/null +++ b/src/go/doc/testdata/examples/whole_file.golden @@ -0,0 +1,21 @@ +-- .Play -- +package main + +import "fmt" + +type X int + +func (X) Foo() { +} + +func (X) TestBlah() { +} + +func (X) BenchmarkFoo() { +} + +func main() { + fmt.Println("Hello, world!") +} +-- .Output -- +Hello, world! diff --git a/src/go/doc/testdata/examples/whole_function.go b/src/go/doc/testdata/examples/whole_function.go new file mode 100644 index 0000000..1754ee3 --- /dev/null +++ b/src/go/doc/testdata/examples/whole_function.go @@ -0,0 +1,13 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package foo_test + +func Foo(x int) { +} + +func Example() { + fmt.Println("Hello, world!") + // Output: Hello, world! +} diff --git a/src/go/doc/testdata/examples/whole_function.golden b/src/go/doc/testdata/examples/whole_function.golden new file mode 100644 index 0000000..7d5b5cb --- /dev/null +++ b/src/go/doc/testdata/examples/whole_function.golden @@ -0,0 +1,11 @@ +-- .Play -- +package main + +func Foo(x int) { +} + +func main() { + fmt.Println("Hello, world!") +} +-- .Output -- +Hello, world! diff --git a/src/go/doc/testdata/examples/whole_function_external.go b/src/go/doc/testdata/examples/whole_function_external.go new file mode 100644 index 0000000..0e0e2f5 --- /dev/null +++ b/src/go/doc/testdata/examples/whole_function_external.go @@ -0,0 +1,12 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package foo_test + +func foo(int) + +func Example() { + foo(42) + // Output: +} diff --git a/src/go/doc/testdata/examples/whole_function_external.golden b/src/go/doc/testdata/examples/whole_function_external.golden new file mode 100644 index 0000000..ec8f114 --- /dev/null +++ b/src/go/doc/testdata/examples/whole_function_external.golden @@ -0,0 +1,8 @@ +-- .Play -- +package main + +func foo(int) + +func main() { + foo(42) +} |