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

Example Analysis of closures in Go language

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the example analysis of closures in Go language. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Closure

Closures are often used in functional programming. What are closures? How did it come into being and what problems did it solve? First, the literal definition of closure is given: a closure is an entity composed of a function and its associated reference environment (that is, closure = function + reference environment). This is literally difficult to understand, especially for programmers who have been programming in imperative languages.

Closures in Go language

Let's take a look at a demo:

Func f (I int) func () int {return func () int {irie + return I}}

The function f returns a function, which is a closure. This function itself does not define the variable I, but refers to the variable I in its environment (function f).

Let's take another look at the effect:

C1: = f (0) c2: = f (0) C1 () / / reference to i, I = 0, return 1c2 () / / reference to another i, I = 0, return 1

C1 and c2 refer to different environments, and the same I is not modified when inotify + is called, so the output is 1 both times. Every time the function f enters, a new environment is formed. In the corresponding closure, the function is all the same function, but the environment refers to a different environment.

Variable I is a local variable in function f. It is not possible to assume that this variable is allocated in the stack of function f. Because after the function f returns, the corresponding stack is invalidated, and the variable I in the function returned by f refers to an invalid position. So variables referenced in the environment of the closure cannot be allocated on the stack.

Escape analyze

Before moving on to the implementation of closures, let's take a look at a language feature of Go:

Func f () * Cursor {var c Cursor c.X = 500noinline () return & c}

Cursor is a structure, which is not allowed in C language, because the variable c is allocated on the stack, and the space of c becomes invalid when the function f returns. However, it is stated in the GE language specification that this method of writing is legal in the GE language. The language automatically recognizes this and allocates memory for c on the heap rather than on the stack of the function f.

To verify this, you can observe the assembly code generated by function f:

MOVQ $type. ".Cursor + 0 (SB), (SP) / / the type of variable c, that is, CursorPCDATA $0memium 16PCDATA $1line 0call, runtime.new (SB) / / calls the new function, which is equivalent to new (Cursor) PCDATA $0mermai 1MOVQ 8 (SP), AX / / takes the address of c.X into the AX register MOVQ $500, (AX) / assigns the value of the memory address stored in AX to 500MOVQ AX,". ~ r0dat24 (FP) ADDQ $16

Recognizing that variables need to be allocated on the heap is implemented by a compiler technology called escape analyze.

If you enter a command:

Go build-gcflags=-m main.go

You can see the output:

Note: in the last two lines, identification c escaped and was moved to the heap. Escape analyze can analyze the scope of variables, which is an important technique for garbage collection.

This is the end of this article on "sample Analysis of closures in Go language". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Development

Wechat

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

12
Report