Go语言学习02-常用集合

本文详细介绍了Go语言中的if条件、switch条件、循环结构(包括for、while),以及数组、切片、Map和Set等数据结构的声明、操作和差异。特别强调了Go语言对循环和条件表达式的独特之处。
摘要由CSDN通过智能技术生成

Go语言学习02-常用集合

条件与循环

循环

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

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

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

代码示例

while条件循环

while (n < 5)

n := 0
for n < 5 {
    n++
    fmt.Println(n)
}

无限循环

while (true)

n := 0
for n < 5 {
    ...
}
if条件
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. 支持变量赋值:

    if var declaration; condition {
        // code to be executed if conditon is true
    }
    
switch条件
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…的逻辑作用等同

数组与切片

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

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

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

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

a := [...]int{12345}
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
切片
内部结构

切片声明
var s0 []int
s0 = append(s0,1)

s := []int{}

s1 := []int{123}

s2 := make([]int24)
   /* []type, len, cap
   其中len个元素会被初始化为默认零值, 未初始化元素不可以访问
   */
切片共享存储结构

数组 vs. 切片
  1. 数组容量不可伸缩
  2. 相同维数, 相同长度的数组可以进行比较, 每一个元素都相同, 这两个数组会被认为相同

Map声明、元素访问及遍历

Map声明
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遍历
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. 基本操作

    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. 元素个数
  • 17
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

残魁斜罡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值