为什么Go在goroutine中对闭包的处理方式不同?

编程入门 行业动态 更新时间:2024-10-09 10:28:25
本文介绍了为什么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中对闭包的处理方式不同?

本文发布于:2023-11-16 11:04:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603702.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中对   方式   goroutine

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!