Definition of a function in Golang

In a programming language, a function is a reusable code segment consisting of a function name, a parameter, and a return value. In Golang, functions fall into two categories: custom functions and system functions. A custom function is a function you write yourself, and a system function is a function of the system itself.

funcThe function name(Parameter list)(List of return values){function bodyreturnReturn value list}Copy the code

Description:

A. Function name: contains letters, digits, and underscores (_). But the first letter of the function name cannot be a number. Function names cannot be the same within the same package;

B. Parameter list: Parameter list is a parameter list. Parameters are composed of parameter variables and parameter types.

C. Return value: The return value consists of the return value type and its variables. You can also write only the return value type

D. Function body: code block to realize specific functions and business logic.

By the way, functions are procedural concepts, which in object-oriented languages are called methods

Function calls in Golang

For functions in the same package, we can call them as function names (arguments).

package main

import (
	"fmt"
)

func main(a) {
	sayHello("Li Feiyu")
	s := getPhoneByName("Nangong Wan")
	fmt.Println(s)
}

func sayHello(name string) {
	fmt.Println("Hello!,name)
}

func getPhoneByName(name string) string {

	return name+"The mobile number is." + "120-6379-3306"
}

Copy the code

Output:

Hello! Li Fei Yu nangong Wan's mobile phone number is120- 6379.- 3306.
Copy the code

In different packages, we can pass the package name. Method name () to call the function.

package myMath

func Add(number ...int) (sum int) {
	sum = 0
	for _, num := range number {
		sum = sum + num
	}
	return
}

Copy the code
package main

import "go_code/function/myMath"

func main(a) {
	sum := myMath.Add(1.2.3)
	print(sum)
}

Copy the code

Output:

6
Copy the code

Advanced usage of parameters

Type shorthand

In Golang, you can abbreviate adjacent arguments if they are of the same type

func sayHello2(name1 ,name2 string)  {
	fmt.Println("Hello!,name1,name2)
}

func main(a) {
	sayHello2("Li Feiyu".Mr.han "")}Copy the code

The output

How do you do! Li Feiyu han LiCopy the code

Variable parameter

A variable parameter is a function that has an unfixed number of arguments. Variable parameters in Golang are defined by adding… To identify.

func sayHello3(name ...string) {
	for _, s := range name {
		fmt.Println("Hello!,s)
	}
}

func main(a) {
	sayHello3("Li Feiyu".Mr.han "")}Copy the code

Output:

Hello! Hello, Li Feiyu! mr.hanCopy the code

Function return value

In Golang, a function can return multiple values. Returns data using the return keyword.

func getPhoneByName(name string) string {

	return name+"The mobile number is." + "120-6379-3306"
}

func getPhoneByName2(name string) (str string){
	str = name+"The mobile number is." + "120-6379-3306"
	return
}

func main(a) {
	
	s1 := getPhoneByName("Nangong Wan")
	fmt.Println(s1)
	s2 := getPhoneByName2("Nangong Wan")
	fmt.Println(s2)
	
}

Copy the code

Output:

Nangong Wan's mobile phone number is 120-6379-3306Copy the code

Description:

In Golang. For return values, the function definition can give the return value a name and return directly

Take functions as arguments

In Golang, functions can be passed as arguments and then called within another function, which is commonly called a callback. Take a look at this example:

package main

import "fmt"

func main(a) {
	name := "Happy little happy"
	sayHello(name,callback)
}

func callback(name string) {
	fmt.Println("hello ",name)
}

func sayHello(name string,f func(name string)) {
	fmt.Println("I am",name)
	f(name)
}

Copy the code

Output:

I am Happy happy happyCopy the code

Defer keywords

The defer keyword allows us to defer executing a statement or function until just before the function returns (or after the return statement executes anywhere).

Look at the following example:

package main

import "fmt"

func main(a) {
	sayHello("Nangong Wan")}func sayHello(name string) {
	fmt.Println("before")
	defer fun1()
	fmt.Println("after")}func fun1(a) {
	fmt.Println("hello world!")}Copy the code

Output:

before
after
hello world!
Copy the code

The use of the defer keyword is similar to the finally keyword in the Java language, which is often used to release some allocated resources. Such as lock release operations, closing database connections, and so on.

Anonymous functions and closures

When we don’t want to name a function, we can use anonymous functions, also known as closures. In Golang, anonymous functions cannot stand alone; you must assign them to a variable (storing the address of the function in the variable) and then call the function by its name. Look at the following example:

package main

import "fmt"

func main(a) {

	fun := func (name string){
		fmt.Println("Hello",name)
	}

	fun("North")}Copy the code

Output:

Hello to the northCopy the code

Closure applications: use functions as return values

Earlier, we said that functions can be passed as arguments, and functions can also be returned as values. Look at the following example:

package main

func main(a) {

	getPhone := sayHello("Li Feiyu")
	
	getPhone()

}

func sayHello(name string) (func(a)) {
	println("Hello!,name)
	return func(a) {
		println("My phone number is: 131****3901")}}Copy the code

Output:

Hello! My mobile phone number is 131****3901Copy the code

In Golang, a function that returns another function can be called a factory function, such as sayHello in the example above. Functions that can return other functions and functions that take other functions as arguments are called higher-order functions, which are characteristic of functional languages.

Write in the last

Golang’s functions are finished here, and examples of this article can be downloaded here. If my study notes can help you, please give me a thumbs up and encouragement. If there are mistakes and omissions in the article, please help to correct them.