Golang understand - function declaration

Normal function declaration

Statement in the form of ordinary function

 
func 函数名(参数列表) (返回参数列表) {
        函数体 
}

Parameter Type shorthand

1. The same type of return value

If the return value is of the same type, enclose the plurality of the return type enclosed, each separated by a comma Return Type

When using the return statement, the order of values ​​in the list of needs and consistent with the function's return value type

The return value is not very pure type of friendship for readability, especially when the same type of return value appears, can not distinguish between the meaning of each return parameter

func subAndSum(x, y int) (int, int) {
    sub := x - y
    sum := x + y
    return sub, sum
}
2. The return value with a variable name
func namedRetValues() (a, b int) {
        a = 1
        b = 2
        return
}

When the return value of function declaration named a, b; can thus return value of the function assignment directly in the body of the function return value of the function named embodiment, a return statement to be displayed before the end of the function is returned.

The same type of return value and return value name only choose one of two forms, a compile error will occur when mixed; for example:

func namedRetValues() (a, b int, int)

Function call

Function call: the return value of the variable list = function name (parameter list)

A plurality of return value of a comma-separated list; for example:

a, b := namedRetValues()

Example:将秒解析为时间单位

package main

// 将秒解析为时间单位

import (
        "fmt"
)

const (
    // 定义每分钟的秒数
    SecondsPerMinute = 60
    // 定义每小时的秒数
    SecondsPerHour = SecondsPerMinute * 60
    // 定义没天的秒数
    SecondsPerDay = SecondsPerHour * 24
)

// 将传入的秒解析为3种时间单位
func resolveTime(seconds int) (day, hour, minute int) {
    day = seconds / SecondsPerDay
    hour = seconds / SecondsPerHour
    minute = seconds / SecondsPerMinute
    // 显示return返回
    return
}

func main() {
    // 直接将返回值作为打印参数
    fmt.Println(resolveTime(1000))
    // 只获取小时和分钟
    _, hour, minute := resolveTime(18000)
    fmt.Println(hour, minute)
    // 只获取天
    day, _, _ := resolveTime(90000)
    fmt.Println(day)
}

Function parameter passing

Go language and return parameters are passed in the call and return值传递

It should be noted that the pointer, slice and map references such as the type of object pointed to content replication does not occur in the parameter passing, but rather 将指针进行复制, to create a similar reference.

Example : Test of golang 函数参数传递为值传递;引用类型参数为一次引用创建

package main
/*
        示例: 测试golang中函数参数传递为值传递; 引用类型参数为一次引用创建
*/

import (
        "fmt"
)

// 用于测试值传递效果的结构体
type Data struct {
    complax []int       // 测试切片在参数传递中的效果, 切片是一种动态类型,内部以指针存在
    instance InnerData  // 实例分配的innerData
    ptr *InnerData      // 将ptr声明为InnerData的指针类型
}

// 代表各种结构体字段; 声明一个内嵌的结构innerData
type InnerData struct {
        a int
}

// 值传递的测试函数; 
// 该函数的参数和返回值都是Data类型,在调用中,Data的内存会被复制后传入函数,当函数返回时,又将返回值复制一次,赋值给函数返回值的接收变量
func passByByValue(inFunc Data) Data {
    // 输出参数的成员情况
    fmt.Printf("in func value: %+v\n", inFunc)
    // 打印inFunc的指针
    fmt.Printf("in func ptr: %p\n", &inFunc)     // 值传递,需要使用&符号取地址; 拥有相同地址且类型相同的变量,表示同一块内存区域

    return inFunc   // 将传入的变量作为返回值返回,返回的过程中将发生值复制
}

// 测试流程,准备一个Data格式的数据结构并填充所有成员,通过调用测试函数,传入Data结构数据,并获取返回值,对比输入和
// 输出后的Data结构数据变化,特别是指针变化情况以及输入和输出整块数据是否被复制

func main() {
    // 准备传入函数的数据结构
    in := Data{
        complax: []int{1, 2, 3},
        instance: InnerData{
          5,
        },
        ptr: &InnerData{1},
        }

    // 输入结构的成员情况
    fmt.Printf("in value: %+v\n", in)

    // 输入结构的指针地址
    fmt.Printf("in ptr: %p\n", &in)

    // 传入结构体,返回同类型的结构体
    out := passByByValue(in)

    // 输出结构的成员情况
    fmt.Printf("out value: %+v\n", out)
    // 输出结构的指针地址
    fmt.Printf("out ptr: %p\n", &out)
}

to sum up

  1. All pointer address Data structure has changed, meaning that all structures are a new memory, whether it is passed in the Data structure internal function, or by returning the value returned Data replication behavior occurs
  2. The value of all the members of the Data structures are not changed, the original value of the transfer, meaning that all parameters are passed by value
  3. Data ptr member structure consistent during the transfer, the transfer function represents a pointer parameter values ​​are passed only in pointer value, pointer part does not replicate

Guess you like

Origin www.cnblogs.com/vinsent/p/11221369.html