This is a creation in Article, where the information may have evolved or changed.
< Strong>package main import " FMT " func main () {i: = 10 closure1: = func () { Fmt. printf ( "closure, I:%d\n" i)//output: i = 15//Key: Here I is a reference to the periphery I, which is the feature of the Golang closure, not a snapshot but a reference, so the modification is to directly change the value of peripheral I,
< Span style= "White-space:pre" > //So the output I value in Main is: +
Less rigorous, but easy to understand: the equivalent of pointers and references in C + +, pointing to the same piece of memory, and plainly sharing I instead of duplicating I. } closure1 () FMT. Printf("in Main, I:%d\n"i)//output: + funcint) { fmt. Printf("function call, pass parameter, I:%d\n"x) } Closure2 (i)//output: 15, This is a call to a value. }
Reference: http://www.goinggo.net/2014/06/pitfalls-with-closures-in-go.html
Note: This article is only my personal notes, if there are errors and omissions, please correct me, study together, my e-mail: htyu_0203_39@sina.com