Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use closure function in go language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use closure functions in go language, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

1. In the past, closure functions were all used to make it easy to write. Recently, I tried to learn from go and found that the closure functions in go are strange, such as the following code:

Package mainimport "fmt" func main () {var list [] func () int for i: = 0; I < 3; iTunes + {list = append (list, func () int {return I})} for _, fun: = range list {fmt.Printf ("% v", fun ())}}

It feels like it should output 0 1 2, but in fact it will output 3 3 3.

Here's what I understand:

1. In the for loop that adds function slices, only the function is added, but the function is not executed. If executed, you need to add () after the function. At the same time, the space where the function is compiled into memory is fixed, so the address of the function should also be fixed, that is, the same function.

2. Although the I variable is in for, the anonymous function is also in for, so the anonymous function can use the I variable. The actual test shows that the address of the I variable in the function is indeed the address of the I variable in the for loop.

3. Before the first for loop exits, it runs iComplete three times, so the final value of the I variable is 3.

4. The second for loop runs the function, in which the I variable is 3 and the natural output is 3.

Anonymous functions do go around a little bit.

2. An example in the simple tutorial of go

Package mainimport "fmt" func getSequence () func () int {iPartition 0 return func () int {iDeposite1 return I} func main () {/ * nextNumber is a function, function I is 0 * / nextNumber: = getSequence () / * call nextNumber function, I variable increments 1 and returns * / fmt.Println (nextNumber ()) / * create a new function nextNumber1 And view the result * / nextNumber1: = getSequence () fmt.Println (nextNumber1 ()) fmt.Println (nextNumber1 ())}

As far as I understand it, the return value of the function getSequence is that the anonymous function func () int,i:=0 is a variable belonging to the getSequence function, so when nextNumber: = getSequence (), this belongs to nextNumber, while nextNumber () runs getSequence's anonymous function func () int, so the value of the I variable is always retained in nextNumber and will be added one at a time as the anonymous function runs.

After reading the above, do you have any further understanding of how to use closure functions in go? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report