自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go 語(yǔ)言的函數(shù)是“一等公民”?

開(kāi)發(fā) 前端
如果對(duì)如何創(chuàng)建和使用它沒(méi)有任何限制:當(dāng)該結(jié)構(gòu)可以被視為沒(méi)有限制的值時(shí),該語(yǔ)言結(jié)構(gòu)被稱為該語(yǔ)言中的 FirstClass 值。

1.介紹

在 Go 語(yǔ)言中,函數(shù)被稱為“一等公民”。實(shí)際上,在其它編程語(yǔ)言中,也有此說(shuō)法,例如 JavaScript。

什么是編程語(yǔ)言的“一等公民”?Ward Cunningham 的解釋如下:

如果對(duì)如何創(chuàng)建和使用它沒(méi)有任何限制:當(dāng)該結(jié)構(gòu)可以被視為沒(méi)有限制的值時(shí),該語(yǔ)言結(jié)構(gòu)被稱為該語(yǔ)言中的 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.

“一等公民”的特性是可以存儲(chǔ)在變量中,可以作為參數(shù)傳遞給函數(shù),可以在函數(shù)中創(chuàng)建并作為返回值從函數(shù)返回。

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 語(yǔ)言的函數(shù)是否符合“一等公民”的特性。

2.存儲(chǔ)在變量中

Go 語(yǔ)言的函數(shù)可以作為變量的值,存儲(chǔ)在變量中。

func main() {
var hello = func(name string) { fmt.Printf("hello %s\n", name) }
hello("gopher")
}

閱讀上面這段代碼,我們定義一個(gè)變量 hello,和一個(gè)匿名函數(shù),將匿名函數(shù)賦值給變量 hello,我們可以通過(guò)變量調(diào)用該匿名函數(shù)。

3.作為參數(shù)傳遞給其他函數(shù)

Go 語(yǔ)言的函數(shù)可以作為參數(shù),傳遞給其他函數(shù)。

https://back-media.51cto.com/editor?id=704975/h6e90be6-6IhK9UNI

閱讀上面這段代碼,我們定義三個(gè)函數(shù),分別是 Circle、areaOfCircle 和 perimeterOfCircle,其中 areaOfCircle 和 perimeterOfCircle 作為 Circle 的參數(shù),分別用于計(jì)算面積和周長(zhǎng)。

4.可以在函數(shù)中創(chuàng)建,并作為返回值

Go 語(yǔ)言的函數(shù)可以在函數(shù)體中創(chuàng)建,并作為返回值從函數(shù)體中返回。

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
}
}

閱讀上面這段代碼,我們定義一個(gè)函數(shù) CircleCalc,在其函數(shù)體中定義兩個(gè)匿名函數(shù),并將匿名函數(shù)作為返回值從函數(shù)體中返回。

5.總結(jié)

本文我們通過(guò)三段示例代碼,證明 Go 語(yǔ)言中函數(shù)符合“一等公民”的特性,我們可以使用這些特性,使業(yè)務(wù)代碼更加簡(jiǎn)潔。

責(zé)任編輯:武曉燕 來(lái)源: Golang語(yǔ)言開(kāi)發(fā)棧
相關(guān)推薦

2023-03-28 07:26:37

2021-03-18 08:54:55

Go 語(yǔ)言函數(shù)

2021-01-27 05:25:44

Go語(yǔ)言函數(shù)

2022-11-07 18:12:54

Go語(yǔ)言函數(shù)

2015-04-27 09:48:46

Kubernetes數(shù)據(jù)中心

2021-04-13 07:58:42

Go語(yǔ)言函數(shù)

2020-11-09 07:25:20

函數(shù) JavaScript數(shù)據(jù)

2022-03-11 10:31:49

Go語(yǔ)音

2022-02-17 16:44:19

函數(shù)Go 語(yǔ)言高階函數(shù)

2011-08-01 14:37:13

2018-03-12 22:13:46

GO語(yǔ)言編程軟件

2021-07-07 21:40:46

Rust函數(shù)勸退

2024-08-26 08:36:26

2020-12-29 06:44:18

GoScala編程語(yǔ)言

2009-03-04 11:22:15

Linux優(yōu)越感高人一等

2023-04-03 08:02:16

切片擴(kuò)容GO

2017-03-10 09:33:16

JavaScript類型

2019-09-26 09:42:44

Go語(yǔ)言JavaPython

2021-01-22 07:48:07

JavaScript 高階函數(shù)閉包

2023-05-06 07:27:47

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)