The basic form of the function definition is as follows.
func function name(argument)Return type{
Contents of processing...
}
It is a flow to take an argument for a function, specify the return value of the function, and write the processing content.
Now let's define a simple function.
main.go
func test(x, y int) int {
a := x * y
return a
}
func main() {
fmt.Println(test(10, 5)) //50
}
The function test is defined by int type x and y, and int type is specified as the return type. The process is assigned to the variable a, the returned value is received by the main function, and the value is specified and output.
Also, multiple return values can be returned.
main.go
func test(x, y int) (int, int) {
a := x * y
b := x / y
return a, b
}
func main() {
a, b := test(10, 5)
fmt.Println(a, b) //50 2
}
You can also define a function without a return value by omitting the return type, as shown below.
main.go
func test() {
fmt.Println("Hello!")
return
}
func main(){
test() //Hello!
}
There are the following idioms for error handling.
main.go
import (
"os"
"log"
)
func main() {
_, err := os.Open("test.txt")
if err != nil {
log.Fatal(err)
}
}
This program is a process that outputs an error message if there is an error when opening a file. Therefore, I am importing the os package and the log package.
Anonymous functions, as the name implies, define functions as "anonymous", unlike named functions that are explicitly defined.
main.go
func main() {
a := func(x, y int) int { return x + y }
fmt.Println(a(2, 5)) //7
}
You can also draw even shorter by taking an argument to the anonymous function as it is.
main.go
func main() {
fmt.Println(func(x, y int) int { return x + y }(2, 5)) //7
}
This prints the same for anonymous function definitions using function literals.
You can return a function in a function by using an anonymous function.
main.go
func test() func() {
return func() {
fmt.Println("output")
}
}
func main() {
a := test()
a() //output
}
This program implicitly assigns a function that has no arguments and no return value as a return value to the variable a and outputs it.
Similarly, it is possible to take a function as an argument.
main.go
func test(a func()) {
a()
}
func main() {
test(func() {
fmt.Println("output") //output
})
}
In this program, ** a function that has neither an argument nor a return value ** is taken as an argument of the function test, and what is called is output by passing it as an anonymous function in the function main.
In Go, anonymous functions are ** closures **, like function closures. Normally, variables defined in a function (local variables) are destroyed after the execution of the function is completed, but by connecting with a closure, it is not destroyed as long as the closure is referenced. There is no. By using this logic, it is possible to realize ** generator-like functions ** that Go does not have.
main.go
func test() func() int {
a := 0 //Local variables referenced from within the closure
return func() int {
a++
return a //Local variables(Variables that belong to closures)Referencing
}
}
func main() {
b := test()
fmt.Println(b()) //1
fmt.Println(b()) //2
fmt.Println(b()) //3
fmt.Println(b()) //4
}
This time, I summarized the functions! In Go, which does not have object-oriented functions, the definition of functions will be central.
From now on, I will summarize the control syntax! !!
Recommended Posts