小编wen*_*liu的帖子

Golang foreach 区别

func Benchmark_foreach1(b *testing.B) {
        var test map[int]int
        test = make(map[int]int)
            for i := 0; i < 100000; i++ {
                        test[i] = 1
            }
            for i := 0; i < b.N; i++ {
                    for i, _ := range test {
                            if test[i] != 1 {
                                    panic("ds")
                            }
                    }
            }
}

func Benchmark_foreach2(b *testing.B) {
            var test map[int]int
            test = make(map[int]int)
            for i := 0; i < 100000; i++ {
                    test[i] = 1
            }
            for i := 0; i < b.N; i++ …
Run Code Online (Sandbox Code Playgroud)

go

2
推荐指数
1
解决办法
284
查看次数

mutex.Lock和deferred mutex.Unock命令

在golang中,sync.Mutex Lock和Unlock是usul操作,但是Lock和defer Unlock的正确顺序是什么?

mu.Lock()
defer mu.Unlock()
Run Code Online (Sandbox Code Playgroud)

要么

defer mu.Unlock()
mu.Lock()
Run Code Online (Sandbox Code Playgroud)

哪个最好?

locking go deferred

-1
推荐指数
1
解决办法
395
查看次数

标签 统计

go ×2

deferred ×1

locking ×1