为什么Go在goroutine中对闭包的处理方式不同? [英] Why does Go handle closures differently in goroutines?

查看:84
本文介绍了为什么Go在goroutine中对闭包的处理方式不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下Go代码(也在 Go Playground 上):

Consider the following Go code (also on the Go Playground):

package main

import "fmt"
import "time"

func main() {
    for _, s := range []string{"foo", "bar"} {
        x := s
        func() {
            fmt.Printf("s: %s\n", s)
            fmt.Printf("x: %s\n", x)
        }()
    }
    fmt.Println()
    for _, s := range []string{"foo", "bar"} {
        x := s
        go func() {
            fmt.Printf("s: %s\n", s)
            fmt.Printf("x: %s\n", x)
        }()
    }
    time.Sleep(time.Second)
}

此代码产生以下输出:

s: foo
x: foo
s: bar
x: bar

s: bar
x: foo
s: bar
x: bar

假设这不是一些奇怪的编译器错误,我很好奇为什么a)s的值在goroutine版本中的解释与在常规func调用中有所不同,以及b)为何将其赋值给s中的局部变量循环在两种情况下都有效.

Assuming this isn't some odd compiler bug, I'm curious why a) the value of s is interpreted differently in the goroutine version then in the regular func call and b) and why assigning it to a local variable inside the loop works in both cases.

推荐答案

Go中的闭包在词法范围内.这意味着闭包内从外部"范围引用的任何变量都不是副本,而是实际上是引用.实际上, for 循环会多次重用同一变量,因此您要在 s 变量的读/写之间引入竞争条件.

Closures in Go are lexically scoped. This means that any variables referenced within the closure from the "outer" scope are not a copy but are in fact a reference. A for loop actually reuses the same variable multiple times, so you're introducing a race condition between the read/write of the s variable.

但是 x 分配了一个新变量(带有:= )并复制了 s ,这导致每个变量都是正确的结果时间.

But x is allocating a new variable (with the :=) and copying s, which results in that being the correct result every time.

通常,最佳做法是传递所需的任何参数,以使您没有引用.示例:

In general, it is a best practice to pass in any arguments you want so that you don't have references. Example:

for _, s := range []string{"foo", "bar"} {
    x := s
    go func(s string) {
        fmt.Printf("s: %s\n", s)
        fmt.Printf("x: %s\n", x)
    }(s)
}

这篇关于为什么Go在goroutine中对闭包的处理方式不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆