Go笔记

抽空学习Go语言。

Go笔记

1 基本变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main

import (
"fmt"
"math"
"unicode/utf8"
)

// run with command:
// go run src\1_variables.go

func main() {
// 3 ways to declare and initialize a variable
fmt.Printf("3 ways to declare and init a variable:\n")

var a int = 100
var b = "100"
c := 0.17

fmt.Printf("a value = %v (%T)\n", a, a)
fmt.Printf("b value = %v (%T)\n", b, b)
fmt.Printf("c value = %v (%T)\n", c, c)
fmt.Println()

// swap variables
fmt.Printf("Easy way to swap variables:\n")
v1 := 1
v2 := 2
fmt.Printf("before swap: v1 = %v (%T), v2 = %v (%T)\n", v1, v1, v2, v2)

v1, v2 = v2, v1
fmt.Printf("after swap: v1 = %v (%T), v2 = %v (%T)\n", v1, v1, v2, v2)
fmt.Println()

/*
Integer
signed: int8, int16, int32, int64
unsigned: uint8, uint16, uint32, uint64
*/
var vUint16 uint16 = math.MaxUint8 + 1
// vUint16 = math.MaxUint16 + 1 // src\1_variables.go:37:9: constant 256 overflows uint16
fmt.Printf("vUint16 = %v (%T)\n", vUint16, vUint16)

var vUint8 uint8 = uint8(vUint16)
fmt.Printf("vUint8 = %v (%T)\n", vUint8, vUint8) // truncated: 0000 0001 (0000 0000)

/*
Float
float32, float64
*/
var vFloat32 float32 = math.E
var vFloat64 float64 = math.E
fmt.Printf("vFloat32 = %f (%T)\n", vFloat32, vFloat32)
fmt.Printf("vFloat64 = %.10f (%T)\n", vFloat64, vFloat64)

/*
Bool
true/false, can not cast to integer types
*/
var vBool bool = true
fmt.Printf("vBool = %v (%T)\n", vBool, vBool)

/*
String
*/
var vStr string = "你好, Go!"
fmt.Printf("vStr = \"%s\" (%T)\n", vStr, vStr)
fmt.Printf("byte len of vStr = %v\n", len(vStr)) // 3*2 + 5*1 = 11, chinese character uses 3 bytes
fmt.Printf("rune len of vStr = %v\n", utf8.RuneCountInString(vStr)) // 7
// traverse each unicode character
for i, h := range vStr {
fmt.Printf("[%v:%c]", i, h)
}
fmt.Println()

/*
Pointer
*/
var ptrStr *string = &vStr
fmt.Printf("ptrStr = %v (%T)\n", ptrStr, ptrStr)
fmt.Printf("*ptrStr = %v (%T)\n", *ptrStr, *ptrStr)

/*
Struct
*/
var vStruct struct {
id int
name string
salary float32
}
vStruct.id = 1
vStruct.name = "Mike"
vStruct.salary = 123.45
fmt.Printf("vStruct = %v (%T)\n", vStruct, vStruct)
}

2 命令行参数flag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import (
"flag"
"fmt"
)

// run with command:
// go run src\2_flag.go --username mike --password 123456 --id 1

func main() {
// argument name, default value, tips
var username *string = flag.String("username", "guest", "a string of username")

var password string
flag.StringVar(&password, "password", "123", "a string of password")

id := flag.Int("id", 0, "an integer of id")

flag.Parse()
fmt.Printf("username = %v, password = %v, id = %v\n", *username, password, *id)
}

3 常量const

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import "fmt"

// run with command:
// go run src\3_const.go

func main() {
const helloStr = "Hello, world!"
const (
name = "Mike"
salary = 123.45
)
fmt.Printf("%v (%T)", name, name)

name = "Tom" // cannot assign to name
fmt.Printf("%v (%T)", name, name)

ptrName := &name // cannot take the address of name
*ptrName = "Tom"
fmt.Printf("%v (%T)", name, name)
}

4 类型type

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main

import "fmt"

// run with command:
// go run src\4_type.go

type aliasInt = int // declare an alias for int
type myInt int // defines a new type

// Person is type of a struct
type Person struct {
name string
age uint8
}

func main() {
var vAliasInt aliasInt
fmt.Printf("vAliasInt = %v (%T)\n", vAliasInt, vAliasInt)

var vMyInt myInt
fmt.Printf("vMyint = %v (%T)\n", vMyInt, vMyInt)

var person Person
person.name = "Mike"
person.age = 20
fmt.Printf("person = %v (%T)\n", person, person)
}

5 if-else条件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main

import (
"flag"
"fmt"
)

// run with command:
// go run src\5_ifelse.go --score 100

func main() {
score := flag.Int("score", -1, "Score of a test")

flag.Parse()
fmt.Printf("score = %v (%T)\n", *score, *score)

if *score < 60 {
fmt.Println("Fail to pass")
} else if *score < 80 {
fmt.Println("Fine")
} else if *score <= 100 {
fmt.Println("Excellent")
} else {
fmt.Println("Wrong score")
}
}

6 switch-case条件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main

import (
"flag"
"fmt"
)

// run with command:
// go run src\6_switchcase.go -score 100 -course CPP

func main() {
score := flag.Int("score", -1, "Score of a test")
course := flag.String("course", "CPP", "Course name")
flag.Parse()

fmt.Printf("score = %v (%T)\n", *score, *score)
fmt.Printf("course = %v (%T)\n", *course, *course)

switch {
case *score < 60:
fmt.Println("fail to pass")
break
case *score < 80:
fmt.Println("fine")
break
case *score <= 100:
fmt.Println("excellent")
break
default:
fmt.Println("wrong score")
}

switch *course {
case "CPP":
fmt.Println("C plus plus")
break
case "PY":
fmt.Println("Python")
break
default:
fmt.Println("Unknown")
}
}

7 for-loop循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import "fmt"

// run with commands:
// go run src\7_forloop.go

// Golang only has for loop
// Golang does not support while and do-while loop

func main() {
for i := 0; i < 10; i++ {
fmt.Println(i)
}
}

8 数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package main

import "fmt"

func main() {
// init array
var colors [3]string
colors[0] = "Red"
colors[1] = "Green"
colors[2] = "Blue"
fmt.Println(colors)

// init array with initial values
languages := [...]string{"C", "C++", "Java"}
fmt.Println(languages)

// init array with new(type), returns a pointer
nations := new([3]string)
nations[0] = "China"
nations[1] = "India"
nations[2] = "Japan"
fmt.Println(*nations)
}

9 切片slice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main

import "fmt"

// run with commands:
// go run src\9_slice.go

// slice is a variable length sequence of data

func main() {
// [...]int{} marks a fixed length array,
// while []int{} declares a variable length slice
slice := []int{1, 2, 3, 4, 5, 6}
printSlice("slice", slice)
printSlice("subSlice", slice[3:])

// slice can be appended with one or more elements
// If it has sufficient capacity, the destination is resliced to accommodate the new elements.
// If it does not, a new underlying array will be allocated.
slice = append(slice, 7, 8)
printSlice("slice", slice)
printSlice("subSlice", slice[3:])

// slice can also be created from an array
arr := [...]int{11, 22, 33, 44, 55}
arrSlice := arr[:3]
printSlice("arrSlice", arrSlice)

// slice can also be dynamically created with make([]T, size, cap)
madeSlice := make([]int, 8, 16)
printSlice("makeSlice", madeSlice)

// slice can be copied from src slice to dest slice
copy(madeSlice, slice)
printSlice("copiedSlice", madeSlice)
}

func printSlice(tag string, slice []int) {
fmt.Printf("%s: date = %v, len = %d, cap = %d, addr = %p\n", tag, slice, len(slice), cap(slice), &slice)
}

10 列表list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main

import (
"container/list"
"fmt"
)

// run with commands:
// go run src\10_list.go

// list.List in go is a doubly linked list

func main() {
// create a list
numsList := list.New()

// append elements to list with PushBack
for i := 1; i < 10; i++ {
numsList.PushBack(i)
}
printList(numsList)

// add elements to front with PushFront
first := numsList.PushFront(0)
printList(numsList)

// elements can be removed
numsList.Remove(first)
printList(numsList)
}

func printList(srcList *list.List) {
for node := srcList.Front(); node != nil; node = node.Next() {
fmt.Print(node.Value, " ")
}
fmt.Println()
}

11 字典map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main

import "fmt"

// run with commands:
// go run src\11_map.go

func main() {
// create a map
emptyMap := map[string]string{}
printMap(emptyMap)

// create and init a map
city2id := map[string]string{
"Suzhou": "0512",
"Beijing": "010",
"Shanghai": "021",
}
printMap(city2id)

// create a map with make(type)
id2city := make(map[string]string)

id2city["0512"] = "Suzhou"
id2city["010"] = "Beijing"
id2city["021"] = "Shanghai"

fmt.Printf("Query: 010, Result: %s\n", id2city["010"])
printMap(id2city)
}

func printMap(srcMap map[string]string) {
fmt.Printf("len = %d\n", len(srcMap))
for k, v := range srcMap {
fmt.Println(k, v, " ")
}
fmt.Println()
}

支持我的写作!(Support my writing!)

欢迎关注我的其它发布渠道