...
1-- Hello.Play --
2package main
3
4import (
5 "fmt"
6)
7
8func main() {
9 fmt.Println("Hello, world!")
10}
11-- Hello.Output --
12Hello, world!
13-- Import.Play --
14package main
15
16import (
17 "fmt"
18 "log"
19 "os/exec"
20)
21
22func main() {
23 out, err := exec.Command("date").Output()
24 if err != nil {
25 log.Fatal(err)
26 }
27 fmt.Printf("The date is %s\n", out)
28}
29-- KeyValue.Play --
30package main
31
32import (
33 "fmt"
34)
35
36func main() {
37 v := struct {
38 a string
39 b int
40 }{
41 a: "A",
42 b: 1,
43 }
44 fmt.Print(v)
45}
46-- KeyValue.Output --
47a: "A", b: 1
48-- KeyValueImport.Play --
49package main
50
51import (
52 "flag"
53 "fmt"
54)
55
56func main() {
57 f := flag.Flag{
58 Name: "play",
59 }
60 fmt.Print(f)
61}
62-- KeyValueImport.Output --
63Name: "play"
64-- KeyValueTopDecl.Play --
65package main
66
67import (
68 "fmt"
69)
70
71var keyValueTopDecl = struct {
72 a string
73 b int
74}{
75 a: "B",
76 b: 2,
77}
78
79func main() {
80 fmt.Print(keyValueTopDecl)
81}
82-- KeyValueTopDecl.Output --
83a: "B", b: 2
84-- Sort.Play --
85package main
86
87import (
88 "fmt"
89 "sort"
90)
91
92// Person represents a person by name and age.
93type Person struct {
94 Name string
95 Age int
96}
97
98// String returns a string representation of the Person.
99func (p Person) String() string {
100 return fmt.Sprintf("%s: %d", p.Name, p.Age)
101}
102
103// ByAge implements sort.Interface for []Person based on
104// the Age field.
105type ByAge []Person
106
107// Len returns the number of elements in ByAge.
108func (a ByAge) Len() int { return len(a) }
109
110// Swap swaps the elements in ByAge.
111func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
112func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
113
114// people is the array of Person
115var people = []Person{
116 {"Bob", 31},
117 {"John", 42},
118 {"Michael", 17},
119 {"Jenny", 26},
120}
121
122func main() {
123 fmt.Println(people)
124 sort.Sort(ByAge(people))
125 fmt.Println(people)
126}
127-- Sort.Output --
128[Bob: 31 John: 42 Michael: 17 Jenny: 26]
129[Michael: 17 Jenny: 26 Bob: 31 John: 42]
View as plain text