Go by example |
1 | fmt.Println("go" + "lang") |
2 | fmt.Println("1+1 =", 1+1) |
3 | fmt.Println("7.0/3.0 =", 7.0/3.0) |
4 | fmt.Println(true && false) |
5 | fmt.Println(true || false) |
6 | fmt.Println(!true) |
7 | var a = "initial" |
8 | fmt.Println(a) |
9 | var b, c int = 1, 2 |
10 | fmt.Println(b, c) |
11 | var d = true |
12 | fmt.Println(d) |
13 | var e int |
14 | fmt.Println(e) |
15 | f := "apple" |
16 | fmt.Println(f) |
17 | const s string = "constant" |
18 | const n = 500000000 |
19 | const d = 3e20 / n |
20 | for i <= 3 { |
21 | for j := 7; j <= 9; j++ { |
22 | for { |
23 | for n := 0; n <= 5; n++ { |
24 | if n%2 == 0 { |
25 | continue |
26 | if num := 9; num < 0 { |
27 | } else if num < 10 { |
28 | switch i { |
29 | case 1: |
30 | switch time.Now().Weekday() { |
31 | case time.Saturday, time.Sunday: |
32 | default: |
33 | whatAmI := func(i interface{}) { |
34 | switch t := t.(type) { |
35 | var a [5]int |
36 | a[4] = 100 |
37 | fmt.Println("len:", len(a)) |
38 | b := [5]int{1, 2, 3, 4, 5} |
39 | var twoD [2][3]int |
40 | var s []string |
41 | s = make([]string, 3) |
42 | fmt.Println("emp:", s, "len:", len(s), "cap:", cap(s)) |
43 | s = append(s, "e", "f") |
44 | c := make([]string, len(s)) |
45 | copy(c, s) |
46 | l := s[2:5] |
47 | l = s[:5] |
48 | l = s[2:] |
49 | t := []string{"g", "h", "i"} |
50 | twoD := make([][]int, 3) |
51 | twoD[i] = make([]int, innerLen) |
52 | m := make(map[string]int) |
53 | delete(m, "k2") |
54 | n := map[string]int{"foo": 1, "bar": 2} |
55 | for _, num := range nums { |
56 | for i, num := range nums { |
57 | for k, v := range kvs { |
58 | for k := range kvs { |
59 | for i, c := range "go" { |
60 | func plus(a int, b int) int { |
61 | func plusPlus(a, b, c int) int { |
62 | func vals() (int, int) { |
63 | func sum(nums ...int) { |
64 | sum(nums...) |
65 | return func() int { |
66 | func zeroptr(iptr *int) { |
67 | fmt.Println("Rune count:", utf8.RuneCountInString(s)) |
68 | runeValue, width := utf8.DecodeRuneInString(s[i:]) |
69 | type person struct { |
70 | func newPerson(name string) *person { |
71 | s := person{name: "Sean", age: 50} |
72 | sp := &s |
73 | fmt.Println(sp.age) |
74 | owner := obj.Owner() |
75 | obj.SetOwner(user) |
76 | if err := file.Chmod(0664); err != nil { |
77 | f, err := os.Open(name) |
78 | if err != nil { |
79 | d, err := f.Stat() |
80 | f.Close() |
81 | for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 { |
82 | a[i], a[j] = a[j], a[i] |
83 | case '0' <= c && c <= '9': |
84 | case ' ', '?', '&', '=', '#', '+', '%': |
85 | Loop: |
86 | break Loop |
87 | func nextInt(b []byte, pos int) (value, nextPos int) { |
88 | defer f.Close() |
89 | defer fmt.Printf("%d ", i) |
90 | p := new(SyncedBuffer) // type *SyncedBuffer |
91 | var v SyncedBuffer // type SyncedBuffer |
92 | func NewFile(fd int, name string) *File { |
93 | return &File{fd, name, nil, 0} |
94 | a := [...]string {Enone: "no error", Eio: "Eio", Einval: "invalid argument"} |
95 | s := []string {Enone: "no error", Eio: "Eio", Einval: "invalid argument"} |
96 | m := map[int]string{Enone: "no error", Eio: "Eio", Einval: "invalid argument"} |
97 | var p *[]int = new([]int) // allocates slice structure; *p == nil; rarely useful |
98 | var v []int = make([]int, 100) // the slice v now refers to a new array of 100 ints |
99 | v := make([]int, 100) |
100 | array := [...]float64{7.0, 8.5, 9.1} |
101 | n, err := f.Read(buf[0:32]) |
102 | type Transform [3][3]float64 // A 3x3 array, really an array of arrays. |
103 | offset := timeZone["EST"] |
104 | seconds, ok = timeZone[tz] |
105 | delete(timeZone, "PDT") |
106 | fmt.Printf("Hello %d ", 23) |
107 | fmt.Fprint(os.Stdout, "Hello ", 23, " ") |
108 | fmt.Println(fmt.Sprint("Hello ", 23)) |
109 | fmt.Printf("%d %x; %d %x ", x, x, int64(x), int64(x)) |
110 | func Printf(format string, v ...interface{}) (n int, err error) { |
111 | func Println(v ...interface{}) { |
112 | func Min(a ...int) int { |
113 | x = append(x, y...) |
114 | gopath = os.Getenv("GOPATH") |
115 | if gopath == "" { |
116 | func (s Sequence) Less(i, j int) bool { |
117 | func (s Sequence) Swap(i, j int) { |
118 | func (s Sequence) Copy() Sequence { |
119 | if str, ok := value.(string); ok { |
120 | type Handler interface { |
121 | type Counter struct { |
122 | func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
123 | import "net/http" |
124 | ctr := new(Counter) |
125 | http.Handle("/counter", ctr) |
126 | type Chan chan *http.Request |
127 | if _, err := os.Stat(path); os.IsNotExist(err) { |
128 | import _ "net/http/pprof" |
129 | m, ok := val.(json.Marshaler) |
130 | if _, ok := val.(json.Marshaler); ok { |
131 | type Reader interface { |
132 | type Writer interface { |
133 | type ReadWriter interface { |
134 | type ReadWriter struct { |
135 | go func() { |
Комментарии