Detailed Explanation of Custom Functions in Golang

Time:2019-4-18

Both process-oriented programming and object-oriented programming are inseparable from the concept of functions, namely, parameters, function names and return values. Next let’s look at how the Go language works in these three areas.

parameter

When it comes to parameters, we need to consider what parameters we should pass or whether we need parameters when we write functions or methods in classes.
Firstly, the parameters are divided into parametric function and parametric function. No parameter means no parameter, so we don’t need to write it.

Have ginseng

Func FunctionTest () { parentheses are used to place parameters
# function body
}

The Go language is a strongly data-typed language. The parameter is to specify the type, otherwise the error will be reported. Func is the declarative keyword of a function.

Func function name (parameter list) return value type {function body interior}

func addTwoNumber(a int, b int) int {
    return a + b
}

Here we write a very simple function that adds up two numbers and returns the result.
Here we should note that the parameter type is written after the parameter name, which is the same as the variable definition.

func addTwoNumber(a int, b int)  (sum int) {
    return a+b
}

The definition of return value can give the return value a name. Another point is that, unlike other programming languages, the return value of a function in Go language can have more than one.

func addTwoNumber(a int, b int)  (int, int) {
    return a+b, a-b
}

You can also give the return value an alias

func addTwoNumber(a int, b int)  (sum, dif int) {
    return a+b, a-b
}

After giving the return value a name like this, you can assign the return value directly.

func addTwoNumber(a int, b int)  (sum, dif int) {
    sum = a+b
    dif = a-b
    return
}

It also returns sum and dif directly.

At the same time, the Go language also has the same function as C language, in which anonymous parameters only occupy places. Not only does the Go language do this, but it also has the function of declaring that variables can occupy places.

func addTwoNumber(int, b int)  (int, int) {
    return b, b
}
func main() {
    a, _, c := 1, 2, 3
    fmt.Println(a, c)
}

Where is used to occupy space, it is not available.
Variable parameter list

func sum(numbers …int) int {
    s := 0
    for i := range numbers{
        s += i
    }
    return s
}
func main() {
    fmt.Println(sum(1,2,3,4,5))
}

In this way, we can pass multiple values to the sum function.

Value Passing, Address Passing in Go

In many languages, when calling functions or methods, it is important to consider whether we are passing values or addresses (in some languages, referencing transfers), and whether we are passing values or addresses.

Let’s use an example of commutative numbers to illustrate:

pass by value

package main

import “fmt”

func swap(x, y int) {
    var temp = x
    x = y
    y = temp
}
func main() {
    a,b := 1,2
    swap(a,b)
    fmt.Println(a, b)
}

Detailed Explanation of Custom Functions in Golang

Address passing

package main

import “fmt”

func swap(a, b *int) {
    var temp = *a
    *a = *b
    *b = temp
}
func main() {
    a,b := 1,2
    swap(&a,&b)
    fmt.Println(a, b)
}

Detailed Explanation of Custom Functions in Golang