Golang anonymous functions and closures

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

Anonymous functions and closures:

Closures and anonymous functions are often used together, and you can use a closed package to access a local variable in a function (the variable being accessed refers to a pointer to a relationship, and the same local variable is manipulated), such as:

func closure ( x int) (func (), func (int)) {fmt. Printf ("Initial value x is:%d, memory address:%p\n", X, &x) F1: = Func () {x = x + 5 FMT. Printf ("x+5:%d, memory Address:%p\n", X, &x)} F2: = func (y int) {x = x + y fmt.    Printf ("x+%d:%d, memory Address:%p\n", y, X, &x)} return F1, F2}func main () {func1, Func2: = Closure (Ten) func1 () Func2 (Ten) Func2} 
The output is:
Initial value x is: 10, memory address: 0xc080000038
X+5:15, memory address: 0xc080000038
X+10:25, memory address: 0xc080000038
x+20:45, memory address: 0xc080000038  

Personal Understanding:

In fact, the most convenient way to understand closures is to consider a closure function as a class, and a closure function call is an instantiation of a class. Unfortunately, I am not yet able to grasp the essence of the concept of closure design and the philosophical ideas it contains.

Reference

Some of the information in the Golang function can be useful in understanding functions and closures in Golang http://www.cnblogs.com/yjf512/archive/2012/12/09/2810313.html
Http://www.ibm.com/developerworks/cn/linux/l-cn-closure/index.html
Http://wklken.me/posts/2014/03/02/05-func.html
http://my.oschina.net/u/698121/blog/157599
http://ieqi.net/2013/03/11/%E7%9C%8B%E8%8C%83%E4%BE%8B%E5%AD%A6-golang-%EF%BC%88%E5%8D%81%E4%BA%94%EF%BC%89-% E9%97%ad%e5%8c%85/http://www.fenby.com/course/units/1015#top

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.