Go learn the function from scratch (a): anonymous functions and closures

Anonymous function:

Anonymous function similar to the function declaration, but there is no function name

func (function parameter list) (function returns a list of values) {

Function internal code

}

In a statement after the function parameters can be added directly call

main FUNC () { 
 G: = FUNC (A int ) uint { // anonymous function 
  return  uint (A + 14 ) 
 } ( 10 ) // after the call statement 
 F: FUNC = (A int ) uint { // anonymous function 
  return  uint (A + 14 ) 
 } 
 Re: = F ( 100 ) 
 fmt.Println (G, Re) 
}

operation result

24 114

 

Closure:

Closure combination of the physical environment is referred to by its associated function (ie: closure function + = reference ambient)

The look is simple anonymous function as a return value

Take an example of the Internet:

Reference: https://www.cnblogs.com/ralap7/p/9195677.html

func squares() func() int {
 var x int
 return func() int {
  x++
  return x * x
 }
}
​
func main() {
 f1 := squares()
 f2 := squares()
​
 fmt.Println("first call f1:", f1())
 fmt.Println("second call f1:", f1())
 fmt.Println("first call f2:", f2())
 fmt.Println("second call f2:", f2())
}

operation result:

first call f1: 1

second call f1: 4

first call f2: 1

second call f2: 4

Here is the function squares, quote environment is the internal squares, though the square f1, f2 are x, but their different environments so the results are the same run twice.

Beginning with the C's I have always wanted to do not understand the thinking closure function in the variable is not assigned to the stack runs on the release yet, later learned to variable escape mechanisms before starting to understand because Go will automatically parse the statement recognizes the role domain because there are external references to x, so that the variable x to escape to the heap rather than stored in the stack, so such an approach is possible.

go build --gcflags=-m main.go Commands can see the variable escape into the heap instead of the stack, so the variables are stored in the function environment.

 

Function of the variable parameters:

Parameter Type: ... interface {}

test1 FUNC (A ... interface {}) 
FUNC test2 (A int , B ... interface {}) // the variable parameters need not be changed on the parameter

When the parameters and the variable parameters immutable simultaneously appear in the function parameter, the variable parameter needs to be placed after the non-variable parameter

May be removed by extraction type assertion and the value range

printTypeValue FUNC (slist ... interface {}) String {
  for _, S: = Range slist {
    // the format type of interface {} string 
   STR: = fmt.Sprintf ( " % V " , S)
    // string describing the type 
   var typeString string 
   // of the type assertion s 
   Switch s (type) {.
    Case  BOOL : // when s is Boolean 
     typeString = " BOOL " 
   Case  string : // when s is a string type 
     = typeString " String "
   Case  int : // when s is an integer type when 
     typeString = " int " 
   } 
 } 
 return typeString 
}

Guess you like

Origin www.cnblogs.com/VingB2by/p/11117726.html