当前位置: 首页 > article >正文

Golang 教程2

Golang 教程2

注意,该文档只适合有编程基础的同学,这里的go教程只给出有区别的知识点

函数的基本形式
//形式
/* 
func 函数(形参列表)(返回值类型列表){
	执行语句
	return + 返回值列表
}
*/

1、 一个返回值的情况
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、没有返回值的情况
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、多个返回值的情况
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、 每个函数执行程序会给这个函数会单独开辟一个内存空间,所以涉及到数值交换的情况下传入地址
// 函数执行完毕之后所对应的内存空间会销毁

//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 不支持重载
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
}

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、 基本数据类型和数据默认都是值传递,即进行值拷贝。在函数内修改,不会影响到原来的值
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中, 函数也是一种数据类型,可以赋值给一个变量,则该变量就是一个数据类型的变量了。通过该变量可以对函数进行调用
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
``````````````````````````````````````````````````````````````````````````````````````````````````



http://www.kler.cn/a/282695.html

相关文章:

  • 2024 CCF中国开源大会“开源科学计算与系统建模openSCS”分论坛成功举办
  • Selective attention improves transformer详细解读
  • 【Zabbix自动化运维监控系列】判断zabbix是主动监控,还是被动监控
  • 使用阿里云快速搭建 DataLight 平台
  • 数据结构小项目
  • Java 包装类
  • 工作 6 年,@Transactional 注解用的一塌糊涂
  • 空间计量 | 空间杜宾误差模型SDEM
  • 基于RK3568平台opencv的图像采集、ffmpeg推流和Windows端拉流(多线程)
  • 新手教学系列——如何实现基于asyncio的高效率 Worker(按需获取任务、防止阻塞与崩溃)
  • 时序预测 | 基于WTC+transformer时间序列组合预测模型(pytorch)
  • 【河北航空-注册安全分析报告-无验证方式导致安全隐患】
  • 图像处理领域的术语解析
  • 【Rust练习】10.元组
  • 【数据结构篇】~链式二叉树(附源码)
  • 两个主机上的Docker容器怎么实现连接
  • 新买的笔记本只有一个C盘,进行磁盘分区的操作
  • 机器人大会引领产业动向,卓翼飞思绘制无人系统教科研新蓝图
  • SpringBoot依赖之PostgreSQL Driver集成
  • Docker搭建 Nexus Maven 私有仓库教程
  • ffmpeg教程及加速视频转码
  • 履带式森林消防车的功能和应用_鼎跃安全
  • 微服务实战系列之玩转Docker(十三)
  • ts转mp4怎么转?分享3个方法,快速搞定
  • 如何用Java SpringBoot+Vue打造摇滚乐鉴赏网站:从设计到实现全解析
  • 【React】为什么Hooks不能出现在判断中