introduction
When you are exposed to language like Go, you may often hear such a sentence. In Go language, functions belong to first-class citizens, and you may be puzzled about what is first-class citizens. Is it because the function has a high priority? If so, what priority? This article will share that functions in Go are the true veils of first-class citizens.
What is a function
Before understanding first-class citizens, let’s first popularize the basics of functions. A function is a block of code that executes a certain specific function. A larger program should generally be divided into several program blocks. Each module is used to implement a specific function. The module here can be called a function.
// Function definitionfunc FunName(Variable name Variable Type...) (Return type...) { return Return value... }
In Go language, a function can be assigned to a variable, which can be used as a parameter of the function, or as a return value of the function. Such behavior can be understood as a function belongs to first-class citizens.
Function usage scenarios
Anonymous functions
Assign a function to a variable in the form of an anonymous function.
package main import ( "fmt" ) func main() { a := func() { ("hello world first class function") } a() ("%T", a) }
In the above program, we assign a function to the variable in line 8. This is the syntax for assigning functions to variables. If you pay close attention, the assigned function has no name. These types of functions are called anonymous functions because they have no names.
Anonymous functions can also be called without assigning them to variables. Let's see how this is done in the example below.
package main import ( "fmt" ) func main() { func() { ("hello world first class function") }() }
In the above program, an anonymous function is defined in line 8, and immediately after the function definition, we call the function using line 10 ("()").
Custom function type
Just like we define our own structure type, we can also define our own function type.
type add func(a int, b int) int
The above code snippet creates a new function type that takes two integer arguments and returns an integer. Now we can define variables of type.
package main import ( "fmt" ) type add func(a int, b int) int func main() { var a add = func(a int, b int) int { return a + b } s := a(5, 6) ("Sum", s) }
In the above program, in line 10, we define a variable type and assign it a function with a signature matching the type. We call the function in line 13 and assign the result to the s variable. The result will be printed:
Sum 11
Functions as parameters
In Go language, it also supports passing functions as parameters of another function.
package main import ( "fmt" ) func simple(a func(a, b int) int) { (a(60, 7)) } func main() { f := func(a, b int) int { return a + b } simple(f) }
In the example above, in line 7, we define a function that accepts a function that takes two int arguments and returns an int as arguments. In the main function on line 12, we create an anonymous function whose signature matches the function's parameters. We call and pass as argument in the next line.
Function as return value
In Go language, a function can also be passed as the return value of another function.
package main import ( "fmt" ) func simple() func(a, b int) int { f := func(a, b int) int { return a + b } return f } func main() { s := simple() (s(60, 7)) }
In the above program, the simple function in line 7 returns a function that takes two parameters and returns.
Closure
Closures are special cases of anonymous functions. A closure is an anonymous function that accesses variables defined outside the body of the function.
package main import ( "fmt" ) func main() { a := 5 func() { ("a =", a) }() }
In the above program, an anonymous function accesses a variable that exists on line 10 outside its body. Therefore, this anonymous function is a closure.
Practical demonstration
The above mentioned some function-related knowledge. Here we use the function as a parameter to demonstrate a demo.
Take the function as an argument and each element in the array is * 5.
package main import "fmt" func sum(array [3]int, f func(int)int) [3]int { newArray := [3]int{0, 0, 0} for k, v := range array { newArray[k] = f(v) } return newAarray } func main() { a := [3]int{1, 2, 3} sum(a, func(n int) int { return n * 5 }) }
The above is the detailed content of how to understand the principle and usage scenarios of Go function as a first-class citizen. For more information about first-class citizens of Go function, please pay attention to my other related articles!