goroutine的调度
goroutine的本质是协程,是实现并行计算的核心。使用go关键字启动一个异步工作的协程,协程相对线程更加轻量化,协程运行在一个或多个线程上。各个协程之间的调度则有程序底层完成(线程和进程的操作是由程序触发系统接口,最后的执行者是系统)。协程拥有自己的寄存器上下文和栈,协程调度切换时 将寄存器和栈保存到其他地方,切换回来时再将寄存器上下文和栈恢复,这样 协程能保留上一次调用时的状态(即所有局部状态的一个特定组合),
2. go struct能不能比较
因为是强类型语言,所以不同类型的结构不能作比较,但是同一类型的实例值是可以比较的,实例不可以比较,因为是指针类型
3. 函数体 多次 defer 或 go defer执行顺序
defer是golang的一个特色功能,被称为“延迟调用函数”。当外部函数返回后执行defer。 执行顺序 先进后出 (类似 栈)
4。 go select 原理 https://blog.csdn.net/u011957758/article/details/82230316
golang 的 select 就是监听 IO 操作,当 IO 操作发生时,触发相应的动作每个case语句里必须是一个IO操作,确切的说,应该是一个面向channel的IO操作
go select 的使用 https://blog.csdn.net/zg_hover/article/details/81453379
5. slice
* array 和 slice 区别 array数组越界 编译出错, slice数组越界 编译时不会报错,运行报 index out of range
type Slice struct {
ptr unsafe.Pointer // Array pointer
len int // slice length
cap int // slice capacity
}
append函数,因为slice底层数据结构是,由数组、len、cap组成,所以,在使用append扩容时,会查看数组后面有没有连续内存快,有就在后面添加,没有就重新生成一个大的素组
//申明切片
slice1 := []int{1, 2, 3, 4}
fmt.Println("初始化")
fmt.Printf("slice1 %p len:%d cap:%d t: %v \n", slice1, len(slice1), cap(slice1), slice1)
slice2 := make([]int, 10)
fmt.Printf("slice2 %p len:%d cap:%d t: %v \n", slice2, len(slice2), cap(slice2), slice2)
slice3 := append(slice1, 5)
fmt.Println("append 重新分配内存")
fmt.Printf("slice3 %p len:%d cap:%d t: %v \n", slice3, len(slice3), cap(slice3), slice3)
slice4 := append(slice3, 6)
fmt.Println("slice3 内存充足 再次append")
fmt.Printf("slice3 %p len:%d cap:%d t: %v \n", slice3, len(slice3), cap(slice3), slice3)
fmt.Printf("slice4 %p len:%d cap:%d t: %v \n", slice4, len(slice4), cap(slice4), slice4)
slice4[0] = 8
fmt.Println("修改slice4 ")
fmt.Printf("slice3 %p len:%d cap:%d t: %v \n", slice3, len(slice3), cap(slice3), slice3)
fmt.Printf("slice4 %p len:%d cap:%d t: %v \n", slice4, len(slice4), cap(slice4), slice4)
//结果:
//slice1 0xc0000ae040 len:4 cap:4 t: [1 2 3 4]
//slice2 0xc0000a0050 len:10 cap:10 t: [0 0 0 0 0 0 0 0 0 0]
//append 重新分配内存
//slice3 0xc0000c8040 len:5 cap:8 t: [1 2 3 4 5]
//slice3 内存充足 再次append
//slice3 0xc0000c8040 len:5 cap:8 t: [1 2 3 4 5]
//slice4 0xc0000c8040 len:6 cap:8 t: [1 2 3 4 5 6]
//修改slice4
//slice3 0xc0000c8040 len:5 cap:8 t: [8 2 3 4 5]
//slice4 0xc0000c8040 len:6 cap:8 t: [8 2 3 4 5 6]
//fmt.Printf("%d", slice2[10]) 错误
6. map
7. go set的实现
用map 的 key不重复, type Set struct { m map[类型]Empty }
8.go 实现消息队列
使用切片加锁可以实现
9。 Go并发模式之 防止goroutine泄漏 https://blog.csdn.net/u013862108/article/details/88629715
10. 使用select完成goroutine中超时功能 https://blog.csdn.net/qq_15437667/article/details/52961671
11. go goroutine 定时器 超时控制 https://cloud.tencent.com/developer/article/1200540
go 问题
https://www.zhihu.com/search?type=content&q=golang
面试题总结 https://blog.csdn.net/yuanqwe123/article/details/81737180 https://blog.csdn.net/ahaotata/article/details/84104437
https://baijiahao.baidu.com/s?id=1623334523226223824&wfr=spider&for=pc