Go语言学习02-常用集合

文章目录

Go语言学习02-常用集合

条件与循环

循环

与其他主要编程语言的差异

Go语言仅支持循环关键字 for

for j := 7; j <= 9; j++

代码示例

while条件循环

while (n < 5)

go 复制代码
n := 0
for n < 5 {
    n++
    fmt.Println(n)
}

无限循环

while (true)

go 复制代码
n := 0
for n < 5 {
    ...
}
if条件
go 复制代码
if condition {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}

if condition-1 {
    // code to be executed if condition-1 is true
} else if condition-2 {
    // code to be executed if condition-2 is true
} else {
    // code to be executed if both condition1 and condition2 are false
}

与其他主要编程语言的差异

  1. condition 表达式结果必须为布尔值

  2. 支持变量赋值:

    go 复制代码
    if var declaration; condition {
        // code to be executed if conditon is true
    }
switch条件
go 复制代码
switch os := runtime.GOOS; os {
case "darwin":
	fmt.Println("OS X.")
// break
case "linux":
	fmt.Println("Linux.")
default:
	// freebsd, openbsd,
	// plan9, windows...
	fmt.Printf("%s.", os)
}

switch {
case 0 <= Num && Num <= 3:
	fmt.Printf("0-3")
case 4 <= Num && Num <= 6:
	fmt.Printf("4-6")
case 7 >= Num && Num <= 9:
	fmt.Printf("7-9")
}

与其他主要编程语言的差异

  1. 条件表达式不限制为常量或者整数;
  2. 单个case中, 可以出现多个结果选项, 使用逗号分隔;
  3. 与C语言等规则相反, Go语言不需要用break来明确退出一个case;
  4. 可以不设定 switch 之后的条件表达式, 在此种情况下, 整个switch结构与多个if...else...的逻辑作用等同

数组与切片

数组的声明
go 复制代码
var a [3]int //声明并初始化为默认零值
a[0] = 1

b := [3]int{1, 2, 3}			// 声明同时初始化
c := [2][2]int{{1,2}, {3, 4}}	// 多位数组初始化

与其他主要编程语言的差异

go 复制代码
func TestTravelArray(t *testing.T) {
    a := [...]{1,2,3,4,5}	// 不指定元素个数
    for idx/*索引*/, elem/*元素*/ := range a {
        fmt.Println(idx, elem)
    }
}
数组截取
go 复制代码
a[开始索引(包含),结束索引(不包含)]

a := [...]int{1,2,3,4,5}
a[1:2] //2
a[1:3] //2,3
a[1:len(a)] //2,3,4,5
a[1:] //2,3,4,5
a[:3] //1,2,3
切片
内部结构
切片声明
go 复制代码
var s0 []int
s0 = append(s0,1)

s := []int{}

s1 := []int{1,2,3}

s2 := make([]int,2,4)
   /* []type, len, cap
   其中len个元素会被初始化为默认零值, 未初始化元素不可以访问
   */
切片共享存储结构
数组 vs. 切片
  1. 数组容量不可伸缩
  2. 相同维数, 相同长度的数组可以进行比较, 每一个元素都相同, 这两个数组会被认为相同

Map声明、元素访问及遍历

Map声明
go 复制代码
m := map[string]int{"one":1, "two":2, "three":3}
m1 := map[string]int{}
m1["one"] = 1
m2 := make(map[string]int, 10 /*Initial Capacity*/)
Map元素的访问

与其他主要编程语言的差异

在访问的Key不存在时, 仍会返回零值, 不能通过返回nil来判断元素是否存在

Map遍历
go 复制代码
m := map[string]int{"one":1, "two":2, "three":3}
for k, v := range m {
    t.Log(k, v)
}

实现Set

Go 的内置集合中没有 Set 实现, 可以map[type]bool

  1. 元素的唯一性

  2. 基本操作

    go 复制代码
    func TestMapForSet(t *testing.T) {
    	mySet := map[int]bool{}
    	mySet[1] = true
    	n := 3
    	if mySet[n] {
    		t.Logf("%d is existing", n)
    	} else {
    		t.Logf("%d is not existing", n)
    	}
    	mySet[3] = true
    	t.Log(len(mySet))
    	delete(mySet, 1)
    	n = 1
    	if mySet[n] {
    		t.Logf("%d is existing", n)
    	} else {
    		t.Logf("%d is not existing", n)
    	}
    }
    1. 添加元素
    2. 判断元素是否存在
    3. 删除元素
    4. 元素个数
相关推荐
m0_6896182811 分钟前
3D打印仿造+ AI大脑赋能,造出会思考的全景相机
笔记·科技·数码相机·3d
Magnetic_h34 分钟前
【iOS】类结构分析
开发语言·笔记·学习·ios·objective-c
wb1891 小时前
Linux火墙管理及优化
linux·运维·笔记·云计算
jerry6094 小时前
LLM笔记(十)vLLM(1)PagedAttention论文笔记
论文阅读·人工智能·笔记·深度学习·学习·transformer
LVerrrr4 小时前
Missashe线代题型总结
笔记·学习·线性代数·考研
崔高杰4 小时前
提升推理能力会丢失指令跟随的能力?——【论文阅读笔记】
论文阅读·笔记
吃着火锅x唱着歌4 小时前
PHP7内核剖析 学习笔记 第八章 命名空间
android·笔记·学习
伊成5 小时前
扫盲笔记之NPM
前端·笔记·npm
hmbbcsm6 小时前
reserve学习笔记(花指令)
笔记·学习
草莓熊Lotso6 小时前
【自定义类型-结构体】--结构体类型,结构体变量的创建和初始化,结构体内存对齐,结构体传参,结构体实现位段
c语言·开发语言·经验分享·笔记·其他