当前位置:网站首页>Go语言中,函数是一种类型
Go语言中,函数是一种类型
2022-07-07 05:25:00 【码二哥】
0 grpc-go、protobuf、multus-cni 技术专栏 总入口
4 grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录
总结(记住下面三句
)
1、
函数
,也是一种类型
(要特别注意
)
2、函数
,可以赋值给变量
(要特别
注意)
3、函数
,可以作为参数
,进行传递
package func5_test
import (
"testing"
"fmt"
)
//声明一个fire函数
func fire() {
fmt.Print("--->this is fire func")
}
func study(str string) {
fmt.Printf("--->I'm studying %s", str)
}
// 测试,函数是否赋值给变量
func TestFuncVariable(t *testing.T) {
// 声明变量f,类型是func类型,函数类型
// 将变量 f 声明为 func() 类型,
// 调用f的时候,实际上调用的是fire函数,此时 f 就被俗称为“回调函数”,
// 此时 f 的值为 nil
var f func()
f = fire
f()
}
// 测试,函数是否赋值给变量
func TestStudy(t *testing.T) {
st := study
st("English")
}
// 测试,函数类型是否可以作为参数进行传递
func learning(f func()) {
fmt.Printf("--->this is running func")
f()
}
func TestFuncParams(t *testing.T) {
f := fire
// 将函数作为参数进行传递
learning(f)
}
边栏推荐
- Leetcode simple question: find the K beauty value of a number
- Transformation function map and flatmap in kotlin
- Interpreting the practical application of maker thinking and mathematics curriculum
- The reified keyword in kotlin is used for generics
- Obsidan之数学公式的输入
- [quick start of Digital IC Verification] 12. Introduction to SystemVerilog testbench (svtb)
- [go ~ 0 to 1] obtain timestamp, time comparison, time format conversion, sleep and timer on the seventh day
- Splunk查询csv lookup table数据动态查询
- 轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
- Interface as a parameter (interface callback)
猜你喜欢
随机推荐
BiSeNet的特点
Function extension, attribute extension and non empty type extension in kotlin
GFS distributed file system
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
Interactive book delivery - signed version of Oracle DBA work notes
Practice of implementing cloud native Devops based on rainbow library app
Qinglong panel - today's headlines
接口作为参数(接口回调)
SSM 整合
Blob object introduction
Register of assembly language by Wang Shuang
IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
Offer harvester: add and sum two long string numbers (classic interview algorithm question)
Golang 编译约束/条件编译 ( // +build <tags> )
Explore creativity in steam art design
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
Wang Zijian: is the NFT of Tencent magic core worth buying?
探索STEAM艺术设计中的创造力
拓维信息使用 Rainbond 的云原生落地实践