有参无返回值函数

package main

import "fmt"

func test0(){

}

func test1(a int8,b string) {
fmt.Println(a)
fmt.Println(b)
}

func test2(args ...int) {//不定参数的写法,不定参数只能放在参数列表最后
for i:=1;i<=len(args);i++{
fmt.Print(args[i-1])
}
fmt.Printf("\n")
}

func test3(args ...int) {
test2(args...)//全部的参数传递给test2
test2(args[:2]...)//切片的方式传递一部分参数给test2
}

func main() {
test0()
test1(99,"hello")
test2(1,2,3,4,7,8)
test3(6,7,8,9,0)
}

有返回值函数

package main

import "fmt"

func test0() int {
a:=666
return a
}
//go官方推荐写法
func test1()(res int){
res=666
return
}

func test2() (int,int,int) {//多个返回值
return 1,2,3
}

func test3() (a,b,c int) {
a=100
b=200
c=300
return
}

func main() {
a:=test0()
b:=test1()
fmt.Println(a)
fmt.Print(b)
c,d,e:=test2()
fmt.Printf("%d,%d,%d",c,d,e)
x,y,z:=test3()
fmt.Printf("%d,%d,%d",x,y,z)
}

函数名首字母小写为private,首字母大写为public