当前位置 : 主页 > 手机开发 > ROM >

[Go] Returning Multiple Values from a Function in Go

来源:互联网 收集:自由互联 发布时间:2021-06-10
Returning multiple values from a function is a common idiom in Go, most often used for returning values along with potential errors. We‘ll go over how to return multiple values from a function and use those values in our program. // You c

Returning multiple values from a function is a common idiom in Go, most often used for returning values along with potential errors. We‘ll go over how to return multiple values from a function and use those values in our program.

 

// You can edit this code!
// Click here and start typing.
package main

import "fmt"

func inc (n int) int {
    return n + 1
}

func timesTwo (n int) int {
    return n * 2
}

func plusOneTimestwo (n int) (int, int) {
    return inc(n), timesTwo(n)
} 

func main() {
    n, m := plusOneTimestwo(3) // assign to variables
    fmt.Println(n, m) // 4, 6
}
网友评论