5.1 Go function definition

1 Go function definition

Go function means: a period of having individual functions code can then be called multiple times elsewhere in the program.

Go into a custom function, system function.

Function can be a big job broken down into smaller tasks.

Function hidden from the user details.

Golang function Features:

支持不定长参数
支持多返回值
支持命名返回参数
支持匿名函数、闭包
函数也是类型,可以赋值给变量

一个package下不得有两个同名函数,不支持函数重载

函数参数可以没有,或者多个参数
注意类型在变量名后面
多个连续的函数命名参数是同一类型,除了最后一个类型,其余可以省略
函数可以返回任意数量的返回值
函数体中,形参作为局部变量
函数返回值可以用 _标识符进行忽略

main()函数由编译器调用,其他函数手动调用

Go function basic syntax:

1) parameter: the input parameters

2) execution of the code: function-code block is realized

3) optional return value of the function

func 函数名(形参列表)(返回值列表){
    执行代码
    return 返回值列表
}

func test(x, y int, z string) (int, string) {
    //类型相同的相邻参数x,y参数类型可以合并
    //多返回值得用括号括起来
    n := x + y
    return n, z
}

1.1. Functions combat

package main

import "fmt"

//最普通的函数,无参数,无返回值
func sayHello() {
    fmt.Printf("hello world\n")
}

//求和函数add
func add(a, b, c int) int {
    //sum := a + b + c
    //return sum
    return a + b
}

//接收2个参数a 和b都是int类型
//返回2个参数,sum和sub作为返回值,也叫做对返回值命名
func calc(a, b int) (sum int, sub int) {
    sum = a + b
    sub = a - b
    return
}

//接收不定长参数个数,
//参数名是b,类型是不固定个数的int类型
//变量b此时是一个slice切片,数据类型:[]int,可以通过索引取值
func calc_v1(b ...int) int {
    sum := 0
    for i := 0; i < len(b); i++ {
        sum = sum + b[i]
    }
    return sum
}

func main() {
    //调用函数
    sayHello()
    //打印返回值求和结果
    fmt.Println(add(5, 5, 5))

    //多个返回值
    sum1, sub1 := calc(5, 10)
    fmt.Printf("calc计算和是%d\n", sum1)
    fmt.Printf("calc计算差是%d\n", sub1)

    //传入不固定长度的参数个数
    sum := calc_v1(10, 20, 30, 40)
    fmt.Println(sum)

}

1.2. Go function Note

1) an array of basic data types and default 值传递, a copy process value, does not modify the original variable

package main

import "fmt"

func modify(n int) {
    n = n + 100
    fmt.Println("modify函数修改后n=", n)
}

func main() {
    num := 10
    modify(num)
    fmt.Println("此时main主程中的nun值=", num)
}

2) If you want to change the variable function can function outside, needs to be 指针传递, the incoming variable 地址, the function to 指针manipulate variables.

package main

import "fmt"

//指针变量,接收一个地址
func modify2(n *int) {
    *n = *n + 100
    fmt.Println("modify2修改后n的值=", *n)
}

func main() {
    num2 := 10
    modify2(&num2)
    fmt.Println("此时main主程中的num2值=", num2)
}

1.3. Init function

Each file will contain a source inti function, which is performed before the main function.

package main

import "fmt"

func init() {
    fmt.Println("init函数一般完成初始化工作,如数据库驱动连接等")
}

func main() {
    fmt.Println("我是主程序")
}

1.4. Init function details

go program loading process:

全局变量
↓
init函数
↓
main函数
package main

import "fmt"

//全局变量
var num = test()

func test() int {
    fmt.Println("执行了test()函数")
    return 999
}

func init() {
    fmt.Println("执行了init函数")
}

func main() {
    fmt.Println("我是主程序")
    fmt.Println(num)
}

Interview questions:

If you package import, main.go and utils.go have variable load, init function execution process?

main.go是程序入口
↓
自上而下,进入import导入
↓
优先进入utils.go 加载全局变量  这是第一步
↓
执行utils.go的init函数        第二步
↓
完毕后,回到main.go的全局变量    第三步
↓
执行main.go的init函数        第四步
↓
执行main.go的主程main()函数    第五步

Guess you like

Origin www.cnblogs.com/open-yang/p/11256832.html