Go面试题

https://blog.csdn.net/weiyuefei/article/details/77963810

1.defer后入先出  panic在defer之后进行执行

2.m[stus[i].Name] = &stus[i]

3.

func main() {
    runtime.GOMAXPROCS(1)
    wg := sync.WaitGroup{}
    wg.Add(20)
    for i := 0; i < 10; i++ {
        go func() {
            fmt.Println("A: ", i)
            wg.Done()
        }()
    }
    for i := 0; i < 10; i++ {
        go func(i int) {
            fmt.Println("B: ", i)
            wg.Done()
        }(i)
    }
    wg.Wait()
典型的例子  A全为10 因为外部变量   B就0-9的数字

5.select

  • select 中只要有一个case能return,则立刻执行。
  • 当如果同一时间有多个case均能return则伪随机方式抽取任意一个执行。
  • 如果没有一个case能return则可以执行”default”块。

6.defer

package main

// 在这个例子中,我们将看到如何使用 Go 协程和通道实现一个工作池 。

import "fmt"


func calc(index string, a, b int) int {
    ret := a + b
    fmt.Println(index, a, b, ret)
    return ret
}
 
func main() {
    a := 1
    b := 2
    defer calc("1", a, calc("10", a, b))
    a = 0
    defer calc("2", a, calc("20", a, b))
    b = 1
}
// 10 1 2 3
// 20 0 2 2
// 2 0 2 2
// 1 1 3 4
// defer是一个栈的结构  前面的就是进来了   数值已经锁定了

7.make

func main() {
    s := make([]int, 5)
    s = append(s, 1, 2, 3)
    fmt.Println(s)
}

[0 0 0 0 0 1 2 3]

s := make([]int, 0)
s = append(s, 1, 2, 3)
fmt.Println(s)

//[1 2 3]

https://blog.csdn.net/yuanqwe123/article/details/81737180

猜你喜欢

转载自blog.csdn.net/fujian9544/article/details/85315983
今日推荐