Golang 教程2

server/2024/12/22 2:07:46/

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 + num2subRes = num1 - num2return 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 := 1b := 2fmt.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 := 1b := 2fmt.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
``````````````````````````````````````````````````````````````````````````````````````````````````
golang__133">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
``````````````````````````````````````````````````````````````````````````````````````````````````
golang_____153">6、 golang 提供可变参数重载 用 “…” 替代
func sum  (name string, isVip bool, args...int)  int { // 函数返回列表只有一个的情况下,可以省略()var sum int = 0fmt.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 = 30fmt.Println(num)
}func main() {num := 10test(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 := 10test(&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 := testfmt.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 - num2addRes = num1 + num2return
}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.ppmy.cn/server/109583.html

相关文章

NV GPU FMA指令测试

NV GPU FMA指令测试 一.小结二.复现步骤1.获取FMA指令的峰值性能、启动开销2.假设固定开销为120个cycle,希望fma pipe利用率超过95%,需要多少条指令呢,求解以下不等式:3.采用1140条fma指令测试4.生成fatbin5.修改SASS指令,删除掉STG.E.STRONG.SYS指令,重新生成fatbin6.准备测试…

学习C语言(19)

整理今天的学习内容 1.memmove使用和模拟实现 void* memmove (void* destination,const void* source,size_t num); 和momcpy的差别是memmove函数处理的源内存块和目标内存块是可以重叠的 memmove的模拟实现: 2.mem…

【HarmonyOS 4.0】@ohos.router 页面路由

注册页面,在src/main/resources/base/profile/main_pages.json文件新增配置。 {"src": ["pages/Index","pages/AnimateTo"] }导入 router 模块 import router from ohos.router1. router.pushUrl 跳转到应用内的指定页面会将当前页面…

使用LinkedHashMap实现固定大小的LRU缓存

使用LinkedHashMap实现固定大小的LRU缓存 1. 什么是LRU? LRU是"Least Recently Used"的缩写,意为"最近最少使用"。LRU缓存是一种常用的缓存淘汰算法,它的核心思想是:当缓存满时,优先淘汰最近最少…

【Vue】Echart渲染数据时页面不显示内容

背景 做的一个对话交互的功能,根据后台返回的数据,渲染成Echart图表展示因为图表种类多,因此根据不同图表单独做了一个个vue组件,将数据根据展示类型传到这些子组件中进行渲染无论哪种图表,第一次展示时都能正常展示&…

SQL 快速参考

SQL 快速参考 引言 SQL(Structured Query Language)是一种用于管理关系数据库管理系统(RDBMS)的标准编程语言。它被广泛用于数据查询、数据更新、数据库维护和访问控制。本快速参考旨在提供SQL的基本概念和常用命令的概览,帮助读者快速理解和应用SQL。 基础概念 数据库…

Sentinel熔断与限流

一、服务雪崩与解决方案 1.1、服务雪崩问题 一句话:微服务之间相互调用,因为调用链中的一个服务故障,引起整个链路都无法访问的情况。 微服务中,服务间调用关系错综复杂,一个微服务往往依赖于多个其它微服务。 如图…

什么是反应诱导重构

反应诱导重构(Reaction-Induced Phase Transformation,RIPT)是一种材料科学中的现象,指的是在特定的反应过程中,材料的晶体结构或相发生了重构或转变。这种现象广泛应用于催化、材料合成和功能材料的研究中。下面是对反…