当前位置:网站首页>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是以包为最小单位,因此同一个包下不能出现同样的变量名和函数名
边栏推荐
- 【小技巧】使用matlab GUI以对话框模式读取文件
- Kotlin basic learning 14
- "Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
- Kotlin基础学习 16
- [ibdfe] matlab simulation of frequency domain equalization based on ibdfe
- Custom classloader that breaks parental delegation
- SQL:常用的 SQL 命令
- 向数据库中存入数组数据,代码出错怎么解决
- Learn more about materialapp and common attribute parsing in fluent
- VS2010插件NuGet
猜你喜欢

Didi open source Delta: AI developers can easily train natural language models

一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)

Which of PMP and software has the highest gold content?

蓝桥杯单片机省赛第七届
![[mv-3d] - multi view 3D target detection network](/img/aa/741b36ead2dfaa5a165401b8d657b7.jpg)
[mv-3d] - multi view 3D target detection network

滴滴开源DELTA:AI开发者可轻松训练自然语言模型

高性能 低功耗Cortex-A53核心板 | i.MX8M Mini

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

Interface debugging tool simulates post upload file - apipost

The 10th Blue Bridge Cup single chip microcomputer provincial competition
随机推荐
MySQL index, transaction and storage engine
Didi open source Delta: AI developers can easily train natural language models
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
Basic syntax of unity script (8) - collaborative program and destruction method
Oracle 常用SQL
Which product of anti-cancer insurance is better?
How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
Xlwings drawing
Vite: configure IP access
First acquaintance with string+ simple usage (II)
[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!
Which of PMP and software has the highest gold content?
How about Ping An lifetime cancer insurance?
蓝桥杯单片机省赛第十二届第二场
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
What kind of interview is more effective?
蓝桥杯单片机第六届温度记录器
What do you know about stock selling skills and principles
Basic operations of MySQL database (based on tables)
FFMpeg AVFrame 的概念.