2.GO- variable parameter functions, anonymous functions and function variables

2.1. The variable parameter function

  • Refers to the number of variable parameters may be any parameter a
  • The variable parameters must be in the final position of the parameter list, add three dots indicate variable parameter function between the parameter name and type
  • When you declare a function in the body of the function as the variable parameters can use slices
package main

import "fmt"

func demo(name string, hover ... string)  {
	fmt.Println (name, "hobby")

	for i,n := range  hover{
		fmt.Println(i,n)
	}
}

func main() {
	demo ( "derek", "read", "play", "Game")
}

//result
derek hobby
0 reading
1 play
2 game

2.2 anonymous function

package main

import "fmt"

func main() {
	// first: None Parameters None Return Values
	func(){
		fmt.Println ( "no parameter None Return Value anonymous function")
	}()

	// second: there are parameters
	func(name string){
		fmt.Println ( "named:", name)
	}("derek")

	// Third: to return to duty
	name := func() string{
		return "zhang_derek"
	}()
	fmt.Println(name)
}

2.3. Function variables

 In the go language function is a type

  • After-defined function variables, you can use an anonymous function assignment, you can use already defined function assignment
  • After defining function variables and the same general function call syntax, variable name is the function name of an ordinary function declaration
  • Function variables are referenced in addition to the fifth slice, map, channel, interface type

 (1) function is a reference type variable

package main

import "fmt"

func b()  {
	fmt.Println("bbb")
}

func main() {
	// function is a reference type variable
	var func ()
	a = b
	// same memory address
	fmt.Println(a,b)     //0x47d820 0x47d820
}

(2) function as a parameter

package main

import "fmt"

func mydo(arg func(name string))  {
	fmt.Println ( "execution mydo")
	arg("derek")
}

func main() {
	mydo(func(name string) {
		fmt.Println(name)
	})
}

(3) function as a return value

package main

import "fmt"

// function as a return value
func a() func() int{
	return func() int {
		return 110
	}
}

func main() {
	result := a()
	r2 := result()
	fmt.Println(r2)    //110
}

 

 
 
 

Source Address: https://www.cnblogs.com/derek1184405959/

2.1. The variable parameter function

  • Refers to the number of variable parameters may be any parameter a
  • The variable parameters must be in the final position of the parameter list, add three dots indicate variable parameter function between the parameter name and type
  • When you declare a function in the body of the function as the variable parameters can use slices
package main

import "fmt"

func demo(name string, hover ... string)  {
	fmt.Println (name, "hobby")

	for i,n := range  hover{
		fmt.Println(i,n)
	}
}

func main() {
	demo ( "derek", "read", "play", "Game")
}

//result
derek hobby
0 reading
1 play
2 game

2.2 anonymous function

package main

import "fmt"

func main() {
	// first: None Parameters None Return Values
	func(){
		fmt.Println ( "no parameter None Return Value anonymous function")
	}()

	// second: there are parameters
	func(name string){
		fmt.Println ( "named:", name)
	}("derek")

	// Third: to return to duty
	name := func() string{
		return "zhang_derek"
	}()
	fmt.Println(name)
}

2.3. Function variables

 In the go language function is a type

  • After-defined function variables, you can use an anonymous function assignment, you can use already defined function assignment
  • After defining function variables and the same general function call syntax, variable name is the function name of an ordinary function declaration
  • Function variables are referenced in addition to the fifth slice, map, channel, interface type

 (1) function is a reference type variable

package main

import "fmt"

func b()  {
	fmt.Println("bbb")
}

func main() {
	// function is a reference type variable
	var func ()
	a = b
	// same memory address
	fmt.Println(a,b)     //0x47d820 0x47d820
}

(2) function as a parameter

package main

import "fmt"

func mydo(arg func(name string))  {
	fmt.Println ( "execution mydo")
	arg("derek")
}

func main() {
	mydo(func(name string) {
		fmt.Println(name)
	})
}

(3) function as a return value

package main

import "fmt"

// function as a return value
func a() func() int{
	return func() int {
		return 110
	}
}

func main() {
	result := a()
	r2 := result()
	fmt.Println(r2)    //110
}

 

Guess you like

Origin www.cnblogs.com/gaidy/p/11864438.html