Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

Spring Recruit punch card day 9 chapter 12.

Today we will continue to update the Go language learning record.

Functions are one of the most important components of any programming language. A function is simply a wrapper around a piece of code: abstract a piece of logic into a function, give it a name, and call it every time you need it. Using functions makes code cleaner and more concise.

define

The following code snippet describes various cases of function definition in the GO language and the use of delay functions.

package main

import "fmt"

// Function definition
func f1(x int, y int) (ret int) {
   return x + y
}

// A function with no return value
func f2(x int, y int) {
   fmt.Println(x + y)
}

// No argument and no return value
func f3(a) {
   fmt.Println("1111")}// No argument has a return value
func f4(a) int {
   return 4
}

// The return value can be named or unnamed
// A named return value is equivalent to declaring a variable in a function
func f5(x int, y int) (ret int) {
   ret = x + y  // Note: since ret is already declared in the return value, use = instead of := to avoid repeating the declaration problem
   return // Since ret is already declared in the function body, there is no need to repeat the declaration when returning
}

// Multiple return values
func f6(a) (int.int) {
   return 1.2
}

// Multiple parameter shorthand
// If the type of the argument is the same, the type of the argument can be omitted. For example:
func f7(x, y, z int, a, b string, c, d bool) int {
   return x + y + z
}

// Variable length argument
// Variable length arguments must be placed at the end of function arguments
func f8(x string, y ...int) {
   fmt.Println(x)
   fmt.Println(y)
}

// defer execution
func deferDemo(a) {
   defer fmt.Println("111") // The statement that first deferred executes last
   defer fmt.Println("222")
   fmt.Println("333")}// The go language has no concept of default parameters for functions
func main(a) {
   r := f5(1.2)
   fmt.Println(r)

   m, n := f6()
   fmt.Println(m, n)

   r7 := f7(1.2.3."1"."1".true.false)
   fmt.Println(r7)

   f8("hah") // Variable length can be left blank
   f8("hah".1.2.3.4)
   
   // Delay function test
   deferDemo()
}
Copy the code

Functions can also be arguments to functions

Here’s an example:

Func f3(x func() int) {ret := x() ftt.printf ("f3 prints ret: Printf("f3 print ret type: %T\n", ret) //2 ftt. Printf("f3 print ret type: %T\n", ret) //int} func main() {a := f2 ftt. Printf("a type: %T\n", a) f3(a)}Copy the code

Print result:

Function as the return value of a function

Package main import "FMT" func f2() int {return 2} func ff(x, y int) int {return x + y} F5 (x func() int) func(int, Int int {return ff} func main() {f7 := f5(f2) FMT.Printf("f7 :%v\n",f7) //f7 returns a function FMT.Printf("f7 type :% T\n", f7)}Copy the code

Print result:

Conclusion:

  1. We print the value of f7 as a memory address
  2. As expected, f7 returns the function type, which we defined as ff()

Anonymous functions

An anonymous function is a function without a name. Anonymous functions are mostly used to implement callbacks and closures.

In Go, functions can no longer be defined inside functions as before, only anonymous functions.

Anonymous functions are defined in the following format:

func(parameters)(Return value){function body}Copy the code

Anonymous functions cannot be called like normal functions because they do not have a function name, so anonymous functions need to be saved to a variable or executed immediately:

func main(a) {
	// Save the anonymous function to a variable
	add := func(x, y int) {
		fmt.Println(x + y)
	}
	add(10.20) // Call an anonymous function through a variable

	// Self-executing functions: Anonymous functions are executed directly after the addition () is defined
	func(x, y int) {
		fmt.Println(x + y)
	}(10.20)}Copy the code

Summary: A self-executing function is an anonymous function followed by an append (), indicating that it is executed without external calls.

closure

A closure is a function that contains a variable in its outer scope

Take a chestnut

package main

import "fmt"

func adder(x int) func(int) int {
   return func(y int) int {
      x += y
      return x
   }
}

func main() {
   f1 := adder(1)
   ret := f1(2)
   fmt.Println(ret)
}
Copy the code

Print result:

Conclusion: The above example is a typical closure: an anonymous function contains an external variable x.

Closure = function + reference environment

conclusion

In this article, we introduced the definition of functions in Go language in detail, and also introduced the knowledge of anonymous functions and closures.

When I finished my review, I felt that closures were a little obscure. If you have a better way to explain closures, feel free to leave a comment.

The last

Thanks for reading and welcome to like, favorites,coin(attention)!!