1、返回数组
package main
import "fmt"
func sumAdd(x ,y int) []int {
var result [] int
result=make([] int,0,6)
for i:=0;i<5;i++{
result=append(result,i+x +y)
}
return result
}
func main() {
fmt.Println(sumAdd(1, 2))
}
2、返回多个值
package main
import "fmt"
func sumAdd(x ,y int) ([]int,int){
var result [] int
var sum int
result=make([] int,0,6)
for i:=0;i<5;i++{
result=append(result,i+x +y)
sum+=i+x +y
}
return result,sum
}
func main() {
fmt.Println(sumAdd(1, 2))
}
[3 4 5 6 7] 25
------------------
(program exited with code: 0)
请按任意键继续. . .
3、不显示指定返回
package main
import "fmt"
func sumAdd(x ,y int) (result []int,sum int){
result=make([] int,0,6)
for i:=0;i<5;i++{
result=append(result,i+x +y)
sum+=i+x +y
}
return
}
func main() {
fmt.Println(sumAdd(1, 2))
}