首页
登录
原
Go 语言的函数
76
人阅读
2020/12/30 10:15
总访问:
155689
评论:
0
手机
收藏
分类:
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) } ```  记录一个执行方法的时间 ------------ ```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 } ``` 
欢迎加群讨论技术,群号:677373950
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
111篇
linux
8篇
linux中cve
1篇
windows中cve
0篇
资源分享
7篇
Win32
2篇
前端
23篇
传说中的c
4篇
Xamarin
1篇
docker
7篇
容器编排
27篇
grpc
4篇
Go
13篇
yaml模板
1篇
理论
1篇
更多
Sqlserver
2篇
云产品
18篇
git
2篇
Unity
1篇
考证
1篇
最新文章
Go 语言可变参数和defer
.net core 3.1 Identity Server4 (添加同意范围页)
.net core 3.1 Identity Server4 集合
.net core 3.1 Identity Server4 (添加HTTPS证书)
.net core 3.1 Identity Server4 (EntityFramework Core 配置)
Elasticsearch 基于词项和基于全文的搜索
Go 语言的函数
Docker 容器逃逸漏洞 (CVE-2020-15257)复现
Go 字符串
Elasticsearch 聚合分析简介
.net core 3.1 Identity Server4 (Hybrid模式)
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2020TNBLOG.NET
技术交流:
群号677373950
欢迎加群
欢迎加群交流技术