This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging

For a long time, I was proud to be part of an Internet technology company. I thought we were different.

Our management is more flat, less bureaucratic, vibrant and youthful. And our products are changing the way people live, and we’re changing the world.

But a series of events in recent years have shaken my confidence and pummelled me: Wake up, brother, it’s not what you think.

What can I do? I don’t know.

Or try to more text, try not to do work as soon as possible.

The function definitions

The function consists of the following sections: the keyword func, the function name, the argument list, the return list, and the function body.

func name(param-list) ret-list {
	body
}
Copy the code

A function can have no arguments or no return value.

func funcA(a) {
	fmt.Println("i am funcA") // i am funcA
}
Copy the code

The type of a function is called a function signature. When two functions have the same argument list and return list, they have the same type or signature.

func add(x int, y int) int {
	return x + y
}

func sub(x int, y int) (z int) {
	z = x - y
	return
}

fmt.Printf("%T\n", add) // func(int, int) int
fmt.Printf("%T\n", sub) // func(int, int) int
Copy the code

parameter

Arguments of multiple adjacent types can be abbreviated, so the add and sub functions can also be written like this:

func add(x, y int) int {
	return x + y
}

func sub(x, y int) (z int) {
	z = x - y
	return
}
Copy the code

Support indefinite parameters, using… Type of grammar. Note that the indefinite argument must be the last argument to the function.

func funcSum(args ...int) (ret int) {
	for _, arg := range args {
		ret += arg
	}
	return
}

// Indeterminate parameters
fmt.Println(funcSum(1.2))    / / 3
fmt.Println(funcSum(1.2.3)) / / 6
Copy the code

You can also pass in slice as an argument, using… Expand slice:

/ / slice parameters
s := []int{1.2.3.4} fmt.Println(funcSum(s...) )/ / 10
Copy the code

In fact, using slice as a parameter can achieve the same effect, but the difference is that when passing the parameter, you have to construct a slice, which is easier to use than indefinite parameters.

func funcSum1(args []int) (ret int) {
	for _, arg := range args {
		ret += arg
	}
	return
}

fmt.Println(funcSum1(s))   / / 10
Copy the code

The return value

A function can return a single value or multiple values.

// Multiple return values
func swap(x, y int) (int.int) {
	return y, x
}

// Multiple return values
fmt.Println(swap(1.2)) 1 / / 2
Copy the code

If there is an unwanted return value, use _ to ignore it:

x, _ := swap(1.2)
fmt.Println(x) / / 2
Copy the code

Support for naming return values. To use a named return value, simply use return without the name of the return value.

The previous example of an indeterminate argument is written in this way:

func funcSum(args ...int) (ret int) {
	for _, arg := range args {
		ret += arg
	}
	return
}
Copy the code

For example, if the return value is not named, it should be:

func funcSum(args ...int) int {
	ret := 0
	for _, arg := range args {
		ret += arg
	}
	return ret
}
Copy the code

Anonymous functions

An anonymous function is an implementation of a function that does not require the definition of a function name. It can be assigned directly to a function variable, used as an argument, returned as a value, or called directly.

// Anonymous function
sum := func(a, b int) int { return a + b }
fmt.Println(sum(1.2)) / / 3
Copy the code

As a parameter:

// Take an anonymous function as an argument
func funcSum2(f func(int.int) int.x.y int) int {
	return f(x, y)
}

fmt.Println(funcSum2(sum, 3.5)) / / 8
Copy the code

As a return value:

// The return value is an anonymous function
func wrap(op string) func(int.int) int {
	switch op {
	case "add":
		return func(a, b int) int {
			return a + b
		}
	case "sub":
		return func(a, b int) int {
			return a + b
		}

	default:
		return nil
	}
}

f := wrap("add")
fmt.Println(f(2.4)) / / 6
Copy the code

Call directly:

// Call directly
fmt.Println(func(a, b int) int { return a + b }(4.5)) / / 9
Copy the code

conclusion

Functions have been used in previous articles, and this article will systematically summarize the points that need to be paid attention to.

This includes function definitions, parameters, returns, and anonymous functions. There is also a closure, implemented by anonymous functions. However, I don’t think closures are used very much, so I didn’t write them. If you are interested in them, you can search them.

Functions can break complex programs into smaller modules, making them more readable, reusable, and maintainable. The ability to abstract specific functionality into functions is essential during development, rather than writing all the code together in a pile of code. This kind of code is not easy to maintain, but the point is that you don’t want to look at it for a long time.


The brain map and source code in the article are uploaded to GitHub, students who need to download.

Address: github.com/yongxinz/go…

List of Go columns:

  1. Development environment setup and VS Code development tools configuration

  2. Declaration and assignment of variables and constants

  3. Basic data types: integer, floating point, complex, Boolean, and string

  4. Composite data types: array and slice

  5. Composite data types: dictionary map and struct

  6. Process control, catch all