首页
视频
资源
登录
原
Go 语言的函数
3900
人阅读
2020/12/30 10:15
总访问:
2592362
评论:
0
收藏:
0
手机
分类:
Go
![go数据类型](https://img.tnblog.net/arcimg/hb/0dd243ae49394f12beed84237bfee1a3.jpg "go数据类型") >#Go 语言的函数 [TOC] 函数与其他语言的主要差异 ------------ >- 可以有多个返回值 - 所有参数都是值传递:slice,map,channel会有传引用的错觉 - 函数可以作为变量的值 - 函数可以作为参数和返回值 Demo ------------ ```go package func_test import ( "math/rand" "testing" "time" ) // 返回一个0-10,0-20的随机数 func returnMultiValues() (int, int) { // 初始化随机数的资源库, 如果不执行这行, 不管运行多少次都返回同样的值 rand.Seed(time.Now().UnixNano()) return rand.Intn(10), rand.Intn(20) } func TestFn(t *testing.T) { // 如果只需要其中的一个值 a, b := returnMultiValues() t.Log(a, b) } ``` ![](https://img.tnblog.net/arcimg/hb/1af25fccbf3e419eada697b99dbea825.png) 记录一个执行方法的时间 ------------ ```go func TestFn(t *testing.T) { // 如果只需要其中的一个值 a, b := returnMultiValues() t.Log(a, b) tsSF := timeSpent(slowFun) t.Log(tsSF(10)) } func timeSpent(inner func(op int) int) func(op int) int { return func(n int) int { // 开启时间 start := time.Now() // 调用函数 ret := inner(n) // 输出秒数 fmt.Println("time spent:", time.Since(start).Seconds()) return ret } } func slowFun(op int) int { time.Sleep(time.Second * 1) return op } ``` ![](https://img.tnblog.net/arcimg/hb/88b56a0182d243abafb6bb755ac3e887.png)
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
168篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
2篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术