### # 包

1. Packages

package main
1

2. Imports 导入程序需要的包，以下是推荐方式。

import (
"fmt"
"math"
)
1
2
3
4

### # 数据类型、变量和常量

1. 数据类型（Types）

bool 布尔类型 Numeric Types 数字类型

• int8, int16, int32, int64, int
• uint8, uint16, uint32, uint64, uint
• float32, float64

string 字符串

2.变量声明

var i, j int = 1, 2
k := 3
1
2

3.常量（Constants）

const World = "世界"
const Truth = true
1
2

### # 复杂数据类型（Array、Slices、Structs和Maps）

1.Arrays

var a [3]int //int array with length 3
a := [3]int{12, 78, 50} // short hand declaration to create array
1
2

package main

import "fmt"

func main() {
a := [...]float64{67.7, 89.8, 21, 78}
fmt.Println("length of a is",len(a))
}
1
2
3
4
5
6
7
8

2.Slices

s := []int{2, 3, 5, 7, 11, 13}
1

Slices有长度length和容量capacity两个函数。length是slices里包含的元素数量，capacity是slices的容量。

package main

import (
"fmt"
)

func main() {
fruitarray := [...]string{"apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"}
fruitslice := fruitarray[1:3]
fmt.Printf("length of slice %d capacity %d", len(fruitslice), cap(fruitslice)) //length of fruitslice is 2 and capacity is 6
}
1
2
3
4
5
6
7
8
9
10
11

package main

import (
"fmt"
"strings"
)

func main() {
// Create a tic-tac-toe board.
board := [][]string{
[]string{"_", "_", "_"},
[]string{"_", "_", "_"},
[]string{"_", "_", "_"},
}

// The players take turns.
board[0][0] = "X"
board[2][2] = "O"
board[1][2] = "X"
board[1][0] = "O"
board[0][2] = "X"

for i := 0; i < len(board); i++ {
fmt.Printf("%s\n", strings.Join(board[i], "  "))
}
}
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

3.structs

go语言没有提供类，使用structs来代表对象。Structs可以被用来存储一个对象，而不是维护一些单独的数据。

type Employee struct {
firstName string
lastName  string
age       int
}
1
2
3
4
5
package main

import (
"fmt"
)

type Employee struct {
firstName, lastName string
age, salary         int
}

func main() {

//creating structure using field names
emp1 := Employee{
firstName: "Sam",
age:       25,
salary:    500,
lastName:  "Anderson",
}

//creating structure without using field names
emp2 := Employee{"Thomas", "Paul", 29, 800}

fmt.Println("Employee 1", emp1)
fmt.Println("Employee 2", emp2)
}
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 (
"fmt"
)

func main() {
emp3 := struct {
firstName, lastName string
age, salary         int
}{
firstName: "Andreah",
lastName:  "Nikola",
age:       31,
salary:    5000,
}

fmt.Println("Employee 3", emp3)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

4.Maps

Map包含了健值对，可以通过key获取对应的value。

type Vertex struct {
Lat, Long float64
}

var m = map[string]Vertex{
"Bell Labs": {40.68433, -74.39967},
}
1
2
3
4
5
6
7
8

package main

import (
"fmt"
)

func main() {
personSalary := make(map[string]int)
personSalary["steve"] = 12000
personSalary["jamie"] = 15000
personSalary["mike"] = 9000
fmt.Println("personSalary map contents:", personSalary)

elem1, ok1 := personSalary["steve"]
fmt.Println("Value:" ,elem1," boolean:" ,ok1)

elem2, ok2 := personSalary["eric"]
fmt.Println("Value:" ,elem2," boolean:" ,ok2)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Last Updated: 3/9/2020, 9:27:52 PM