func cal (num1 int, num2 int){//注意,此处省略了返回列表的()
fmt.Println(num1 + num2)}funcmain(){cal(10,20)}``````````````````````````````````````````````````````````````````````````````````````````````````(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01>go run .\main.go3``````````````````````````````````````````````````````````````````````````````````````````````````
3、多个返回值的情况
func cal (num1 int, num2 int)(addRes int, subRes int){// 函数返回只有一个的情况下,可以省略()
addRes = num1 + num2
subRes = num1 - num2
return addRes, subRes
}funcmain(){
addRes, subRes :=cal(10,20)//返回多少个值就用多少个变量接收//如果不需要某个返回值,用 "_" 接收进行忽略//如 addRes, _ := cal(10, 20)
fmt.Printf("add result is %d, sub result is %d", addRes, subRes)}``````````````````````````````````````````````````````````````````````````````````````````````````(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01>go run .\main.go
add result is 30, sub result is -10``````````````````````````````````````````````````````````````````````````````````````````````````
4、 每个函数执行程序会给这个函数会单独开辟一个内存空间,所以涉及到数值交换的情况下传入地址
// 函数执行完毕之后所对应的内存空间会销毁//a、 不传入变量地址的情况func swap (num1 int, num2 int){// 函数返回只有一个的情况下,可以省略()
num1, num2 = num2, num1
}funcmain(){
a :=1
b :=2
fmt.Printf("before change, a is %d, b is %d \n", a, b)swap(a, b)
fmt.Printf("after change, a is %d, b is %d", a, b)}``````````````````````````````````````````````````````````````````````````````````````````````````(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01>go run .\main.go
before change, a is 1, b is 2
after change, a is 1, b is 2``````````````````````````````````````````````````````````````````````````````````````````````````//b、 传入变量地址的情况func swap (num1 *int, num2 *int){// 函数返回只有一个的情况下,可以省略()*num1,*num2 =*num2,*num1
}funcmain(){
a :=1
b :=2
fmt.Printf("before change, a is %d, b is %d \n", a, b)swap(&a,&b)
fmt.Printf("after change, a is %d, b is %d", a, b)}``````````````````````````````````````````````````````````````````````````````````````````````````(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01>go run .\main.go
before change, a is 1, b is 2
after change, a is 2, b is 1``````````````````````````````````````````````````````````````````````````````````````````````````
5、 golang 不支持重载
func swap (num1 int, num2 int){// 函数返回只有一个的情况下,可以省略()
num1, num2 = num2, num1
}func swap (num1 int){// 函数返回只有一个的情况下,可以省略()}``````````````````````````````````````````````````````````````````````````````````````````````````(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01>go run .\main.go
# command-line-arguments
.\main.go:12:6: swap redeclared in this block //其他语言可以支持,go不支持.\main.go:8:6: other declaration of swap
``````````````````````````````````````````````````````````````````````````````````````````````````
6、 golang 提供可变参数重载 用 “…” 替代
func sum (name string, isVip bool, args...int)int{// 函数返回列表只有一个的情况下,可以省略()var sum int=0
fmt.Println(name, isVip, args)for_, value :=range args {// 遍历数组, 如果不需要索引, 用 _ 承接
sum += value
}return sum
}funcmain(){
fmt.Println(sum("liMing",true,1,2,3,4,5,6,7,8,9))}``````````````````````````````````````````````````````````````````````````````````````````````````(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01>go run .\main.go
liMing true[123456789]45``````````````````````````````````````````````````````````````````````````````````````````````````
7、 基本数据类型和数据默认都是值传递,即进行值拷贝。在函数内修改,不会影响到原来的值。
func test (num int){
num =30
fmt.Println(num)}funcmain(){
num :=10test(num)
fmt.Println(num)}``````````````````````````````````````````````````````````````````````````````````````````````````(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01>go run .\main.go3010``````````````````````````````````````````````````````````````````````````````````````````````````//如何才能让main函数里头的num值也发生改变? 可以传入地址func test (ptr *int){
fmt.Println(ptr)*ptr =30//这里的意思是将指针对应的值进行改变
fmt.Println(*ptr)}funcmain(){
num :=10test(&num)// 传入的是地址
fmt.Println(num)}``````````````````````````````````````````````````````````````````````````````````````````````````(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01>go run .\main.go0xc00000a0f83030``````````````````````````````````````````````````````````````````````````````````````````````````