阿彪温馨的家
我不去想是否能够成功,既然选择了远方,便只顾风雨兼程。

6、If/Else

Go 中分支是直截了当的。ifelse

请注意,在 Go 中,您不需要在条件两边加上括号,但大括号是必需的。

在 Go 中没有三元组,因此即使对于基本条件,您也需要使用完整的语句。

$ go run switch.go 
Write 2 as two
It's a weekday
It's after noon
I'm a bool
I'm an int
Don't know type string

7、Switch

Switch跨多个分支表达条件。

可以使用逗号分隔同一语句中的多个表达式。

 

$ go run arrays.go
emp: [0 0 0 0 0]
set: [0 0 0 0 100]
get: 100
len: 5
dcl: [1 2 3 4 5]
2d:  [[0 1 2] [1 2 3]] 

8、Arrays

在 Go 中,数组是特定长度的元素的编号序列。

 

$ go run arrays.go
emp: [0 0 0 0 0]
set: [0 0 0 0 100]
get: 100
len: 5
dcl: [1 2 3 4 5]
2d:  [[0 1 2] [1 2 3]]

9、Slices

切片是 Go 中的关键数据类型,为序列提供了比数组更强大的接口。

 

$ go run slices.go
emp: [  ]
set: [a b c]
get: c
len: 3
apd: [a b c d e f]
cpy: [a b c d e f]
sl1: [c d e]
sl2: [a b c d e]
sl3: [c d e f]
dcl: [g h i]
2d:  [[0] [1 2] [2 3 4]]

10、Maps

Maps是 Go 的内置关联数据类型(有时在其他语言中称为哈希字典)。

 

$ go run maps.go 
map: map[k1:7 k2:13]
v1:  7
len: 2
map: map[k1:7]
prs: false
map: map[bar:2 foo:1]
 

 

posted on 2021-11-30 09:06  甩掉裤衩凭风吹  阅读(44)  评论(0编辑  收藏  举报