Golang中函数的多返回值的三种写法
第一种方法
func fool(a string, b int) int {
fmt.Println("fool1------------")
fmt.Println("a1 = ", a)
fmt.Println("b1 = ", b)
c := 100
return c
}
第二种方法
//返回多个返回值,形参匿名的
func fool2(a string, b int) (int, int) {
fmt.Println("fool2------------")
fmt.Println("a2 = ", a)
fmt.Println("b2 = ", b)
return 12, 33
}
第三种方法
//效果一样
//func fool3(a string, b int) (r1,r2 int) {
//返回多个返回值,有形参名称的
func fool3(a string, b int) (r1 int, r2 int) {
fmt.Println("fool3------------")
fmt.Println("a3 = ", a)
fmt.Println("b3 = ", b)
//r1 r2输入fool3的形参,初始化默认的值是0
//r1 r2 作用域空间是 fool3 整个函数体的{}空间
fmt.Println("r1 = ",r1)
fmt.Println("r2 = ",r2)
//给有名称的返回值变量赋值
r1 = 1000
r2 = 2000
return
}
实现部分
func main() {
c := fool("abc", 555)
fmt.Println("c = ", c)
ret1, ret2 := fool2("hah", 999)
fmt.Println("ret1 = ", ret1, ",ret2 = ", ret2)
ret1, ret2 = fool3("hah111", 222)
fmt.Println("ret1 = ", ret1, ",ret2 = ", ret2)
}
输出