Golang 教程2
注意,该文档只适合有编程基础的同学,这里的go教程只给出有区别的知识点
函数的基本形式
go
复制代码
//形式
/*
func 函数(形参列表)(返回值类型列表){
执行语句
return + 返回值列表
}
*/
1、 一个返回值的情况
go
复制代码
func cal (num1 int, num2 int) (int) { // 函数返回只有一个的情况下,可以省略(), 如果没有返回值,后面一个括号可以直接不写。
return num1 + num2
}
func main() {
fmt.Println(cal(1, 2))
}
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
3
``````````````````````````````````````````````````````````````````````````````````````````````````
/*
注意
1、遵循标识符命名规范:见名知意 + 驼峰命名, 如addNum
2、首字母大写则可以被本包文件和其他包文件使用(类似public)
3、首字母小写只能被本包文件使用,其他包文件不能使用(类似于private)
*/
2、没有返回值的情况
go
复制代码
func cal (num1 int, num2 int) { //注意,此处省略了返回列表的()
fmt.Println(num1 + num2)
}
func main() {
cal(10, 20)
}
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
3
``````````````````````````````````````````````````````````````````````````````````````````````````
3、多个返回值的情况
go
复制代码
func cal (num1 int, num2 int) (addRes int, subRes int) { // 函数返回只有一个的情况下,可以省略()
addRes = num1 + num2
subRes = num1 - num2
return addRes, subRes
}
func main() {
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、 每个函数执行程序会给这个函数会单独开辟一个内存空间,所以涉及到数值交换的情况下传入地址
go
复制代码
// 函数执行完毕之后所对应的内存空间会销毁
//a、 不传入变量地址的情况
func swap (num1 int, num2 int) { // 函数返回只有一个的情况下,可以省略()
num1, num2 = num2, num1
}
func main() {
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
}
func main() {
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 不支持重载
go
复制代码
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 提供可变参数重载 用 "..." 替代
go
复制代码
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
}
func main() {
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 [1 2 3 4 5 6 7 8 9]
45
``````````````````````````````````````````````````````````````````````````````````````````````````
7、 基本数据类型和数据默认都是值传递,即进行值拷贝。在函数内修改,不会影响到原来的值。
go
复制代码
func test (num int) {
num = 30
fmt.Println(num)
}
func main() {
num := 10
test(num)
fmt.Println(num)
}
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
30
10
``````````````````````````````````````````````````````````````````````````````````````````````````
//如何才能让main函数里头的num值也发生改变? 可以传入地址
func test (ptr *int) {
fmt.Println(ptr)
*ptr = 30 //这里的意思是将指针对应的值进行改变
fmt.Println(*ptr)
}
func main() {
num := 10
test(&num) // 传入的是地址
fmt.Println(num)
}
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
0xc00000a0f8
30
30
``````````````````````````````````````````````````````````````````````````````````````````````````
8、 在go中, 函数也是一种数据类型,可以赋值给一个变量,则该变量就是一个数据类型的变量了。通过该变量可以对函数进行调用
go
复制代码
func test (num int) {
fmt.Println(num)
}
//可以通过type 给函数取别名
type myFunc func(int)
func test2 (num int, num2 float32, testFunc myFunc) {
testFunc(123)
}
func main() {
a := test
fmt.Printf("a 的类型是: %T, test函数的类型是: %T\n", a, test)
//可以通过该变量对函数进行调用
a(123) // 等价于 test(123)
test2(123, 3.14, test)
test2(123, 3.14, a)
}
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
a 的类型是: func(int), test函数的类型是: func(int)
123
123
123
``````````````````````````````````````````````````````````````````````````````````````````````````
// 可以对返回的列表变量取别名, 这样函数体里面的顺序就无所谓了
func test (num1 int, num2 int) (addRes int, subRes int) {
subRes = num1 - num2
addRes = num1 + num2
return
}
func main() {
addRes, subRes := test(10, 20)
fmt.Println(addRes, subRes)
}
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
30 -10
``````````````````````````````````````````````````````````````````````````````````````````````````