| 1 |
-- Hello.Play -- |
| 2 |
package main |
| 3 |
|
| 4 |
import ( |
| 5 |
"fmt" |
| 6 |
) |
| 7 |
|
| 8 |
func main() { |
| 9 |
fmt.Println("Hello, world!") |
| 10 |
} |
| 11 |
-- Hello.Output -- |
| 12 |
Hello, world! |
| 13 |
-- Import.Play -- |
| 14 |
package main |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"log" |
| 19 |
"os/exec" |
| 20 |
) |
| 21 |
|
| 22 |
func 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 -- |
| 30 |
package main |
| 31 |
|
| 32 |
import ( |
| 33 |
"fmt" |
| 34 |
) |
| 35 |
|
| 36 |
func 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 -- |
| 47 |
a: "A", b: 1 |
| 48 |
-- KeyValueImport.Play -- |
| 49 |
package main |
| 50 |
|
| 51 |
import ( |
| 52 |
"flag" |
| 53 |
"fmt" |
| 54 |
) |
| 55 |
|
| 56 |
func main() { |
| 57 |
f := flag.Flag{ |
| 58 |
Name: "play", |
| 59 |
} |
| 60 |
fmt.Print(f) |
| 61 |
} |
| 62 |
-- KeyValueImport.Output -- |
| 63 |
Name: "play" |
| 64 |
-- KeyValueTopDecl.Play -- |
| 65 |
package main |
| 66 |
|
| 67 |
import ( |
| 68 |
"fmt" |
| 69 |
) |
| 70 |
|
| 71 |
var keyValueTopDecl = struct { |
| 72 |
a string |
| 73 |
b int |
| 74 |
}{ |
| 75 |
a: "B", |
| 76 |
b: 2, |
| 77 |
} |
| 78 |
|
| 79 |
func main() { |
| 80 |
fmt.Print(keyValueTopDecl) |
| 81 |
} |
| 82 |
-- KeyValueTopDecl.Output -- |
| 83 |
a: "B", b: 2 |
| 84 |
-- Sort.Play -- |
| 85 |
package main |
| 86 |
|
| 87 |
import ( |
| 88 |
"fmt" |
| 89 |
"sort" |
| 90 |
) |
| 91 |
|
| 92 |
// Person represents a person by name and age. |
| 93 |
type Person struct { |
| 94 |
Name string |
| 95 |
Age int |
| 96 |
} |
| 97 |
|
| 98 |
// String returns a string representation of the Person. |
| 99 |
func (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. |
| 105 |
type ByAge []Person |
| 106 |
|
| 107 |
// Len returns the number of elements in ByAge. |
| 108 |
func (a ByAge) Len() int { return len(a) } |
| 109 |
|
| 110 |
// Swap swaps the elements in ByAge. |
| 111 |
func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 112 |
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age } |
| 113 |
|
| 114 |
// people is the array of Person |
| 115 |
var people = []Person{ |
| 116 |
{"Bob", 31}, |
| 117 |
{"John", 42}, |
| 118 |
{"Michael", 17}, |
| 119 |
{"Jenny", 26}, |
| 120 |
} |
| 121 |
|
| 122 |
func 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] |