细节之go
创建时间: 2025-11-11 21:03:32修改时间: 2025-11-12 13:06:07
前言
快毕业了,现在一直在找工作,之前写代码感觉牛逼plus。go语言我才花了一个星期就学完了,结果到了现在找工作面试,面试官一问三不知。。。只会无脑CURD。哎。。。看来还是太菜了,所以这次就总结一下面试的问题叭。
问题1
问go语言defer的执行顺序。
答:先出后进(LIFO),最后执行return,案例如下:
func fn() (i string) {
i = "x"
defer func() {
i = i + "x"
}()
fmt.Println("1")
defer fmt.Println("2")
fmt.Println("3")
defer fmt.Println("4")
fmt.Println("5")
return i
}
最终输出:
1
3
5
4
2
xx
问题2
go语言切片扩容机制
答:首先我们得知道go语言的切片底层还是数组,只是对数组的一个引用。slice有两个容量获取的方法,len:长度,cap:容量。cap就是切片底层数组分配的空间。所以下方表达式永远成立:
0 <= len(s) <= cap(s)
但是面试官问的具体扩容多少,百度很多博客都说是1.2倍,但是实际我并没有看到go语言官方有对这个做解释,下面我写了段代码分析了一下这个行为。
arr := make([]int, 1)
arr = append(arr, 0)
fmt.Println("len: ", len(arr), "cap: ", cap(arr))
arr = make([]int, 10)
arr = append(arr, 0)
fmt.Println("len: ", len(arr), "cap: ", cap(arr))
arr = make([]int, 100)
arr = append(arr, 0)
fmt.Println("len: ", len(arr), "cap: ", cap(arr))
arr = make([]int, 10000)
arr = append(arr, 0)
fmt.Println("len: ", len(arr), "cap: ", cap(arr))
运行结果:
len: 2 cap: 2 # 小容量时,cap空间甚至没有翻倍
len: 11 cap: 20 # 10个时就已经翻了一倍
len: 101 cap: 224 #100时,就是网上说的1.2但是实际情况并不是
len: 10001 cap: 13312 # 这个肯定大于1.2了,也就是说go的slice在底层扩容时,原本的空间如果很大,他扩容的只会越来越大
问题3
go语言context包
答:go语言官方对这个包的解释是:包上下文定义了带有截止时间的上下文类型, 取消信号和其他跨 API 边界的请求范围值和进程之间。这个context中分别有4中上下文类型,分别为:
| 函数名 | 参数 | 返回值 | 作用 |
|---|---|---|---|
| WithCancel | parent context | (ctx Context, cancel CancelFunc) | 取消协程 |
| WithDeadline | parent Context, d time.Time | (Context, CancelFunc) | 截至时间取消协程 |
| WithTimeout | parent Context, timeout time.Duration | (Context, CancelFunc) | 过去时间取消协程 |
| WithValue | parent Context, key, val any | Context | 协程间传递参数 |
WithCancel
示例代码:
var wg sync.WaitGroup
func main() {
start := time.Now()
ctx, cancel := context.WithCancel(context.Background())
wg.Go(func() {
i := 0
for {
select {
case <-ctx.Done():
return
default:
{
i++
fmt.Println("i:", i)
time.Sleep(time.Second)
}
}
}
})
wg.Go(func() {
x := 0
for {
select {
case <-ctx.Done():
return
default:
{
x++
fmt.Println("x:", x)
time.Sleep(time.Second)
}
}
}
})
// 两秒后取消context
time.Sleep(time.Second * 2)
cancel()
wg.Wait()
fmt.Println("执行时间:", time.Since(start))
}
运行结果:
x: 1
i: 1
i: 2
x: 2
执行时间: 2.001383127s
在上述结果中可以看到ctx.Done()可以控制无数个协程。
WithDeadline
实例代码:
var wg sync.WaitGroup
func main() {
start := time.Now()
// 当前时间3秒后停止
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second*3))
wg.Go(func() {
i := 0
for {
select {
case <-ctx.Done():
return
default:
{
i++
fmt.Println("i:", i)
time.Sleep(time.Second)
}
}
}
})
wg.Go(func() {
x := 0
for {
select {
case <-ctx.Done():
return
default:
{
x++
fmt.Println("x:", x)
time.Sleep(time.Second)
}
}
}
})
wg.Go(func() {
// 两秒后取消context
time.Sleep(time.Second * 5)
cancel()
})
time.Sleep(time.Second * 4)
wg.Go(func() {
x := 0
for {
select {
case <-ctx.Done():
fmt.Println("ok")
return
default:
{
x++
fmt.Println("xx:", x)
time.Sleep(time.Second)
}
}
}
})
wg.Wait()
fmt.Println("执行时间:", time.Since(start))
}
运行结果:
i: 1
x: 1
i: 2
x: 2
x: 3
i: 3
ok
执行时间: 5.000689457s
可以在上述案例中看到,3秒后计数就被停止了,但是在5秒后调用cancel()后不会有任何报错等提示。并且在这个ctx完成后怎么调用ctx.Done()都会立即返回结束。
WithTimeout
实例代码:
var wg sync.WaitGroup
func main() {
start := time.Now()
// 3秒后停止
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
wg.Go(func() {
i := 0
for {
select {
case <-ctx.Done():
return
default:
{
i++
fmt.Println("i:", i)
time.Sleep(time.Second)
}
}
}
})
wg.Go(func() {
x := 0
for {
select {
case <-ctx.Done():
return
default:
{
x++
fmt.Println("x:", x)
time.Sleep(time.Second)
}
}
}
})
wg.Go(func() {
// 两秒后取消context
time.Sleep(time.Second * 5)
cancel()
})
time.Sleep(time.Second * 4)
wg.Go(func() {
x := 0
for {
select {
case <-ctx.Done():
fmt.Println("ok")
return
default:
{
x++
fmt.Println("xx:", x)
time.Sleep(time.Second)
}
}
}
})
wg.Wait()
fmt.Println("执行时间:", time.Since(start))
}
运行结果:
i: 1
x: 1
x: 2
i: 2
x: 3
i: 3
ok
执行时间: 5.000882587s
其实这个context和WithDeadline本质上没有任何区别,只不过是时间单位不一样。
WithValue
示例代码
package main
import (
"context"
"fmt"
"sync"
"time"
)
var wg sync.WaitGroup
func main() {
start := time.Now()
// 3秒后停止
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
ctx = context.WithValue(ctx, "o", "hello")
wg.Go(func() {
i := 0
for {
select {
case <-ctx.Done():
return
default:
{
i++
if i == 2 {
fmt.Println("i==2")
ctx = context.WithValue(ctx, "ox", "ox")
}
fmt.Println("i:", i)
time.Sleep(time.Second)
}
}
}
})
wg.Go(func() {
x := 0
for {
select {
case <-ctx.Done():
return
default:
{
o := ctx.Value("o")
x++
fmt.Println("x:", x, "o: ", o, "ox: ", ctx.Value("ox"))
time.Sleep(time.Second)
}
}
}
})
wg.Go(func() {
// 两秒后取消context
time.Sleep(time.Second * 5)
cancel()
})
time.Sleep(time.Second * 4)
wg.Go(func() {
x := 0
for {
select {
case <-ctx.Done():
fmt.Println("ok")
return
default:
{
x++
fmt.Println("xx:", x)
time.Sleep(time.Second)
}
}
}
})
wg.Wait()
fmt.Println("执行时间:", time.Since(start))
}
运行效果:
i: 1
x: 1 o: hello ox: <nil>
x: 2 o: hello ox: <nil>
i==2
i: 2
i: 3
x: 3 o: hello ox: ox
ok
执行时间: 5.000892372s
在上述效果中ox是运行时传输的,意味着ctx是可以后期追加的。
总结
本来想写个总结的,但是我也不知道写啥,感觉挺费时间的,我还是先去改我的论文叭。
