当前位置:网站首页>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是以包为最小单位,因此同一个包下不能出现同样的变量名和函数名
边栏推荐
- Kotlin基础学习 14
- Kotlin basic learning 17
- regular expression
- [personal notes] PHP common functions - custom functions
- Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
- It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
- Account management of MySQL
- Interface debugging tool simulates post upload file - apipost
- Didi open source Delta: AI developers can easily train natural language models
- Review materials of project management PMP high frequency examination sites (8-1)
猜你喜欢
JVM知识点
Jetpack之LiveData扩展MediatorLiveData
NLog use
The 11th Blue Bridge Cup single chip microcomputer provincial competition
MySQL之账号管理
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
微信小程序中 在xwml 中使用外部引入的 js进行判断计算
PY3, PIP appears when installing the library, warning: ignoring invalid distribution -ip
《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
SQL Yiwen get window function
随机推荐
Kotlin basic learning 14
Basic operations of MySQL database (based on tables)
How should the team choose the feature branch development mode or trunk development mode?
[Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)
What is the logical structure of database file
蓝桥杯单片机省赛第十二届第二场
《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)
Kotlin basic learning 13
leetcode-1380. Lucky number in matrix
[mv-3d] - multi view 3D target detection network
A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
Pycharm2021 delete the package warehouse list you added
The 9th Blue Bridge Cup single chip microcomputer provincial competition
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
JVM知识点
The fourth provincial competition of Bluebridge cup single chip microcomputer