Golang function Two (anonymous function and closure)

Source: Internet
Author: User
Tags mul gopher
This is a creation in Article, where the information may have evolved or changed.

An anonymous function is a function that does not have a function name defined. We can define an anonymous function inside a function, also called a function nesting.

An anonymous function can be called directly, or it can be assigned to a variable, as a parameter, or as a return value. Like what:

Func Listen main () {Listen and listen to Func (s listen to string) {Listen and listen///directly be called Listen hear listen listen to hear println (s) listen to hear} ("Hello Listen to Gopher!!!") Listen to/listen to/Hear the Func (S-listen string) {Listen to hear listen//not be called Listen listen listen hear println (s) Listen listen to listen to hear */}func listen to listen to listening to hear hear the listen listening to listens to listens to listen to hear listening to the sound of a listening and hearing Assigning variables to the variable listen listen listen hear listen listen to listen to hear hi ("Hello listen to Gopher!!!") (println)} Func Listen Test (f Listen to Func (string)) {Listen to listen f ("Hello Listen gopher!!!")} Func Listen main () {Listen to listen hi listen: = Listen to Func (s listen to string) {Listen and listen listen to hear println listen and listen to listen to hear} listen and listen to listen to hear the sound of listening and listening./As parameter}func listen to test () func (string) { Listen to listen hi listen: = Listen to Func (S-listen string) {Listen listen//As return value Listen listen listen hear listen to listen to listen to hear println listen to listen to hear the Hi}func hear the main () {Listen to listen F listen to listen to: Hello Listen gopher!!! ")}

Both normal and anonymous functions can be used as fields for structs, such as:

{Listen and listen to the type listen to struct{listen to listen to hear listen to listen to hear mul listen to listen to hear and listen to listen to hear listen to hear the x listen to listening and listening, listen to the listening and listening: Listen to listen to hear listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen to listen Listen, listen, listen, listen, hear, listen, x*y, listen and listen, listen, listen, hear, hear, listen, listen println (X.mul (2,3))}

They can also be passed through channel channels, such as:

{Listen to listen to C listen: = Listen to make (Chan listen to func (int, listen int) int, listen to 2) Listen listen C listen to <-listen to the func (x, y listen int) Listen int Listen {return listen x Listen + Listen y} listen to hear println ((<-c) (2,3) )}

Closures (closure)

Closures are blocks of code (functions) that refer to a free variable (not bound to a particular object) in context, or a combination of a code block (function) and a reference environment. Like what:

Func Listen Intseq () func () int{Listen listen to listen I listen: = Listen 0 Listen listen to listen to hear the println (&i) listen to listen to listen to the func () int{Listen listen listen to listen to listen to hear I listen to listen to listen to the 1 listen and listen to hear the println (&i, i) Listen, listen, listen, listen, hear, listen and listen. Listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, hear, hear, hear, hear, hear, hear, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, hear, hear, hear, hear, hear, hear, Println (Nextint ()) Listen and listen to the FMT. Println (Nextint ()) Listen and listen to the FMT. Println (Nextint ()) Listen and listen to Newint listen: Listen to Intseq () listen to the FMT. Println (Newint ())} output: 0xc42000a3200xc42000a320 Listen to 110xc42000a320 listen 220xc42000a320 listen to 330xc42007a0100xc42007a010 listen to 11

When the Nextint function returns, with the output pointer, we can see that the function is still referencing the original environment variable pointer while it is running, which is called closure. Therefore, closures are a combination of function and reference environment variables.

Because closures refer to environment variables by pointers, they cause the life cycle of the variable

and is even allocated to heap memory. If multiple anonymous functions refer to the same environment variable, it can complicate things, such as:

Func listen test () []func () listen to listen to the Var listen to listen to []func () listen to listen to listen to hear for i:= listen to hear the listen to listen to hear the 3;i++{listen to listening < Listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, hear, hear, println, listen to, listen and listen. () listen to the listen and hear the return listen to the S}func hear listen to listen to listen to listen to listen to listen to listen to listen to listen to hear to listen to listen to listen to Listen to funcslice{listen to listen to listen f () listen to listen to the output: 0xc42000a320 listen 30xc42000a320 listen to 30xc42000a320 3

The workaround is to assign values to different environment variables or parameters each time, such as the modified test function:

Func listen test () []func () listen to listen to the Var listen to listen to []func () listen to listen to listen to hear for listening to i:= listen to listen to listening to listen to hear the 3;i++{listen to listening to hear listen to hear listen to listen to hear listening to listen to hear listening to listen to listening to hear s listen Listen, listen, listen, hear, hear, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, hear, listen, listen, listen, println, hear, hear, listen.

Closures can read and modify environment variables without passing parameters, and of course we have to pay for this traversal, so in daily development, high concurrency services

scenario, it is advisable to use caution unless you are clear that your needs must be done.

This article from "learned in the text, about the ceremony" blog, reproduced please contact the author!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.