前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go 语言的函数是“一等公民”?

Go 语言的函数是“一等公民”?

作者头像
frank.
发布2022-03-31 18:20:19
6750
发布2022-03-31 18:20:19
举报

大家好,我是 frank。

01

介绍

在 Go 语言中,函数被称为“一等公民”。实际上,在其它编程语言中,也有此说法,例如 JavaScript。

什么是编程语言的“一等公民”?Ward Cunningham 的解释如下:

如果对如何创建和使用它没有任何限制:当该结构可以被视为没有限制的值时,该语言结构被称为该语言中的 FirstClass 值。

A language construct is said to be a FirstClass value in that language when there are no restrictions on how it can be created and used: when the construct can be treated as a value without restrictions.

“一等公民”的特性是可以存储在变量中,可以作为参数传递给函数,可以在函数中创建并作为返回值从函数返回。

FirstClass features can be stored in variables, passed as arguments to functions, created within functions and returned from functions. In dynamically typed languages, a FirstClass feature can also have its type examined at run-time.

本文我们介绍一下 Go 语言的函数是否符合“一等公民”的特性。

02

存储在变量中

Go 语言的函数可以作为变量的值,存储在变量中。

代码语言:javascript
复制
func main() {
 var hello = func(name string) { fmt.Printf("hello %s\n", name) }
 hello("gopher")
}

阅读上面这段代码,我们定义一个变量 hello,和一个匿名函数,将匿名函数赋值给变量 hello,我们可以通过变量调用该匿名函数。

03

作为参数传递给其他函数

Go 语言的函数可以作为参数,传递给其他函数。

代码语言:javascript
复制
func main() {
 area := Circle(5, areaOfCircle)
 fmt.Printf("area:%.2f\n", area)

 perimeter := Circle(5, perimeterOfCircle)
 fmt.Printf("perimeter:%.2f\n", perimeter)
}

func Circle(r float64, op func(float64) float64) float64 {
 return op(r)
}

func areaOfCircle(r float64) float64 {
 return math.Pi * r * r
}

func perimeterOfCircle(r float64) float64 {
 return 2 * math.Pi * r
}

阅读上面这段代码,我们定义三个函数,分别是 Circle、areaOfCircle 和 perimeterOfCircle,其中 areaOfCircle 和 perimeterOfCircle 作为 Circle 的参数,分别用于计算面积和周长。

04

可以在函数中创建,并作为返回值

Go 语言的函数可以在函数体中创建,并作为返回值从函数体中返回。

代码语言:javascript
复制
func main() {
 calcArea := CircleCalc("area")
 fmt.Println(calcArea(5))
 calcPerimeter := CircleCalc("perimeter")
 fmt.Println(calcPerimeter(5))
}

func CircleCalc(s string) func(float64) float64 {
 switch s {
 case "area":
  return func(r float64) float64 {
   return math.Pi * r * r
  }
 case "perimeter":
  return func(r float64) float64 {
   return 2 * math.Pi * r
  }
 default:
  return nil
 }
}

阅读上面这段代码,我们定义一个函数 CircleCalc,在其函数体中定义两个匿名函数,并将匿名函数作为返回值从函数体中返回。

05

总结

本文我们通过三段示例代码,证明 Go 语言中函数符合“一等公民”的特性,我们可以使用这些特性,使业务代码更加简洁。

关于自定义函数类型和闭包,我们在之前的文章中已经介绍过,限于篇幅,本文不再赘述,感兴趣读者朋友们,可以翻阅一下之前的文章。

参考资料: https://wiki.c2.com//?FirstClass https://en.wikipedia.org/wiki/First-class_function https://golangbot.com/first-class-functions/ https://dev.to/andyhaskell/basics-of-first-class-functions-in-go-d5p https://go.dev/doc/codewalk/functions/ https://levelup.gitconnected.com/first-class-functions-in-golang-ef2a5001bb4f https://stackoverflow.com/questions/4358031/first-class-functions-in-go https://livebook.manning.com/book/get-programming-with-go/chapter-14/ https://dave.cheney.net/2016/11/13/do-not-fear-first-class-functions

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-03-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Go语言开发栈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档