当前位置:网站首页>go 函数
go 函数
2022-07-02 03:45:00 【UPythonFish】
go 函数
函数是一块执行特定任务的代码。一个函数是在输入源基础上,通过执行一系列的算法,生成预期的输出。
在go语言中,函数是第一等公民,这点同python一样,都可以把函数当做变量值使用
函数中的参数列表和返回值并非是必须的
函数基础
定义函数的基础语法如下:
func 关键字 函数名(形参1 形参1的类型, 形参2 形参2的类型)(返回值类型){
函数体的内容(跟缩进无关,只要是大括号内,都是函数体内容)
}
// 在python中,函数需要先定义再使用,而在go中,只要函数定义过即可使用
func main() {
fmt.Println("hello world")
test() // 调用函数test
}
func test() {
fmt.Println("hello test")
}
当函数需要参数时,应定义好参数的类型,并且只能按位置传参,没有python的关键字参数
func main() {
test1(1,2)
}
func test1(a int, b int){
sum := a + b
fmt.Println(sum)
}
当函数有放回值时,应提前定义好返回值的类型,并且支持多返回值,并且调用函数必须接收改返回值
func main() {
sum := test2(1,2) // 推荐写法, 也可以用其他定义变量方式如: var sum int = test2(1,2)
sum1, b := test3(3,4)
sum2, b2 := test4(5,6)
fmt.Println(sum,sum1,b)
fmt.Println(sum2, b2)
}
func test2(a int, b int)(int){
// 只有一个返回值,可以不加 (),但是多个返回值必须加
sum := a + b
return sum
}
func test3(a int, b int)(int, int){
// 一个返回值定义一个类型
sum := a + b
return sum, b
}
func test4(a int, b int)(sum int,b2 int){
// 相当于已经定义了 sum 和 b2 两个变量
sum = a + b
b2 = b
return // 不需要明确指定返回值,默认返回 sum, b2 的值
}
空白符
_ 在 Go 中被用作空白符,可以用作表示任何类型的任何值,作用同python中一样。使用频率很高
func main() {
a,_,_ := test5()
_,b,_ := test5()
fmt.Println(a,b)
}
func test5()(int,int,int){
return 5,6,7
}
函数高级
1 可变长参数
跟python不一样,python有可变长位置参数和可变长关键字参数,go只有可变长位置参数 (…)
func main() {
test6(1,2,3,4,5)
}
func test6(a ...int) {
fmt.Println(a) // [1 2 3 4 5]
}
2 匿名函数
定义在函数内部的函数, 在go中必须是匿名函数
// 基本的匿名函数
func test7(){
func (){
fmt.Println("基本匿名函数")}()
}
// 有参数的匿名函数
func test7(){
func(a,b int) {
fmt.Println("有参数的匿名函数")
}(4,5)
}
// 有参数,有返回值的匿名函数
func test7(){
a :=func(a,b int)int{
return a+b
}(3,4)
fmt.Println(a)
}
3 函数参数
在go语言中,函数是第一等公民,这点同python一样,都可以把函数当做变量值,赋值给一个变量,并且传给函数,当作返回值使用
func test7(){
f := func(){
// 推荐写法 也可 var f func() =
fmt.Println("函数赋值")
}
fmt.Printf("f的类型是:%T",f) // 是这个类型 func()
f() // 调用函数
}
// 闭包函数定义, go语言也可以同python一样制作装饰器,但是没有语法糖,所以基本不用
1. 定义在函数内部的函数
2. 对外部函数有引用
func test8() {
a := 10
func(){
fmt.Println(a)
}()
}
4 给类型重命名
因为go中函数是一等公民,可以传给变量,因此go中函数传参类型可以写的非常复杂,案例如下
func main() {
a := test9()
a1, f := a(1,test8) // 我是内层函数 10
fmt.Println(a1) // 1
f() // 头晕了
}
func test8() {
a := 10
func(){
fmt.Println(a)
}()
}
func test9() func(a int,b func()) (int,func()){
a:=func(a int,b func()) (int,func()){
fmt.Println("我是内层函数")
b()
return a, func() {
fmt.Println("头晕了")
}
}
return a
}
test9() func(a int,b func()) (int,func()) 解析
test9() 函数名
func(a int,b func()) test9 的函数参数
(int,func()) test9 的放回值类型
这时候就可以来给函数重命名
// 把func(a int,b func()) (int,func()) 类型,重命名为MyFunc
type MyFunc func(a int,b func()) (int,func())
func test9() MyFunc{
a:=func(a int,b func()) (int,func()){
fmt.Println("我是内层函数")
b()
return a, func() {
}
}
return a
}
byte 和 rune 就是这样实现的
type byte uint8
type rune int32
补充:
- go是以包为最小单位,因此同一个包下不能出现同样的变量名和函数名
边栏推荐
- The 9th Blue Bridge Cup single chip microcomputer provincial competition
- The fourth provincial competition of Bluebridge cup single chip microcomputer
- 蓝桥杯单片机数码管技巧
- Jetpack's livedata extension mediatorlivedata
- Which product of anti-cancer insurance is better?
- 高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
- [personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
- 【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真
- The 11th Blue Bridge Cup single chip microcomputer provincial competition
- 2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有
猜你喜欢

接口调试工具模拟Post上传文件——ApiPost
![[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!](/img/46/d36ae47c3d44565d695e8ca7f34980.jpg)
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!

Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
![[untitled] basic operation of raspberry pie (2)](/img/b4/cac22c1691181c1b09fe9d98963dbf.jpg)
[untitled] basic operation of raspberry pie (2)

Basic syntax of unity script (6) - specific folder

The 9th Blue Bridge Cup single chip microcomputer provincial competition

Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini

蓝桥杯单片机第四届省赛

【DesignMode】原型模式(prototype pattern)

Which of PMP and software has the highest gold content?
随机推荐
蓝桥杯单片机省赛第十一届第一场
Influence of air resistance on the trajectory of table tennis
Unity脚本的基础语法(8)-协同程序与销毁方法
Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)
Kotlin basic learning 15
Interface debugging tool simulates post upload file - apipost
[yolo3d]: real time detection of end-to-end 3D point cloud input
Vite: configure IP access
【DesignMode】原型模式(prototype pattern)
Class design basis and advanced
Jetpack's livedata extension mediatorlivedata
Introduction to Robotics II. Forward kinematics, MDH method
蓝桥杯单片机省赛第十一届
The fourth provincial competition of Bluebridge cup single chip microcomputer
毕设-基于SSM电影院购票系统
Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
SQL Yiwen get window function
[punch in] flip the string (simple)
The page in H5 shows hidden execution events
接口调试工具模拟Post上传文件——ApiPost