1.返回单个值
2.返回多个值,匿名
3.返回多个值,形参
代码:
package main
import "fmt"
//
func foo1(a string,b int) int{
fmt.Println("a=",a)
fmt.Println("b=",b)
c:=100
return c
}
//返回多个值,匿名
func foo2(a string,b int) (int,int) {
fmt.Println("a=",a)
fmt.Println("b=",b)
return 666,777
}
//返回多个值,有行参名称的
func foo3(a string,b int)(r1 int,r2 int){
fmt.Println("-----foo3--------")
fmt.Println("a=",a)
fmt.Println("b=",b)
//r1 r2 属于foo3的形参,初始化默认值为0
fmt.Println("r1=",r1)
fmt.Println("r2=",r2)
r1=1000
r2=2000
return
}
func foo4(a string,b int)(r1,r2 int){
fmt.Println("-----foo4--------")
fmt.Println("a=",a)
fmt.Println("b=",b)
r1=1000
r2=2000
return
}
func main(){
c:=foo1("abc",555)
fmt.Println("c=",c)
ret1,ret2:=foo2("haha",99)
fmt.Println("ret1=",ret1,"ret2=",ret2)
ret1,ret2:=foo3("foo3",33)
fmt.Println("ret1=",ret1,"ret2=",ret2)
ret1,ret2:=foo3("foo4",44)
fmt.Println("ret1=",ret1,"ret2=",ret2)
}