Go language-make traps and closure functions

Source: Internet
Author: User
Tags closure

Go language make Traps
a := make([]int, 3)a = append(a, 1, 2, 3)切片大小变成6
anonymous functions

An anonymous function is a function implementation that does not need to define a function name and can be directly assigned to a variable or run directly

Closed Package
Go的匿名函数是一个闭包,闭包是可以包含自由(未绑定到特定对象)变量的代码块,这些变量不在这个代码块内或者,任何全局上下文中定义,而是在定义代码块的环境中定义。要执行的代码块(由于自由变量包含,在代码块中,所以这些自由变量以及它们引用的对象没有被释放)为自由变量提供绑定的计算环境(作用域)。

The value of a closure is that it can be used as a function object or as an anonymous function.

Closures are literal definitions: Closures are entities that are composed of functions and their associated reference environments (that is, closures = functions + reference environments). This is literally hard to understand, especially for programmers who have been programming with imperative languages.

The life cycle of a variable in a closure is the same as the period of a closure variable

A closure in go, a function and an entity that is combined with its associated reference environment.

Go has a variable of a function type, so that although you cannot declare another function directly in a function, you can declare a variable of a function type in a function, where the function is called closure (closure).

Example 1:
package mainimport "fmt"func getSequence() func(i int) int {            sum:=0             return func(i int) int {                sum = sum +i                return sum    }}func main(){        nextNumber := getSequence()        fmt.Println(nextNumber(1))        fmt.Println(nextNumber(1))        fmt.Println(nextNumber(1))        nextNumber1 := getSequence()        fmt.Println(nextNumber1(1))        fmt.Println(nextNumber1(1))}

Run results

12312
Example 2
package mainimport "fmt"func getSequence() func() int {    sum:=0    return func() int {                sum += 1                return sum             }}func main(){        nextNumber := getSequence()        fmt.Println(nextNumber())        fmt.Println(nextNumber())        fmt.Println(nextNumber())        nextNumber1 := getSequence()        fmt.Println(nextNumber1())        fmt.Println(nextNumber1())}

Run results

12312
Example 3
package mainimport "fmt"func main(){        sum1 := 10        getSequence := func() func() int {                     sum:=0                     return func() int {                                fmt.Println(sum1)                                sum += 1                                return sum                     }        }        nextNumber := getSequence()        fmt.Println(nextNumber())        fmt.Println(nextNumber())        fmt.Println(nextNumber())        nextNumber1 := getSequence()        fmt.Println(nextNumber1())        fmt.Println(nextNumber1())        fmt.Println("end:",sum1)}

Run results

101102103101102end: 10

Go language-make traps and closure functions

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.