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

函數(shù)是一等公民,這到底在說什么?

開發(fā) 后端
對于有些人來說這根本不是問題,但有些人卻想不明白。我提到,在 Go 語言中,函數(shù)是一等公民,但對方不清楚這到底在說什么??磥碛斜匾忉屜率裁词且坏裙?。

[[378588]]

在 Go 語言中文網(wǎng)微信群里有人問了這么一個問題:(要加群記得在公眾號回復(fù)消息“入群”)

來自群友的問題

請問下各位大佬,這是什么語法,為什么不需要參數(shù)的?

對于有些人來說這根本不是問題,但有些人卻想不明白。我提到,在 Go 語言中,函數(shù)是一等公民,但對方不清楚這到底在說什么??磥碛斜匾忉屜率裁词且坏裙?。

再往下看之前,你能說出什么是一等公民嗎?

關(guān)于一等公民[1](First-class citizen)看看維基百科的定義:

In programming language design, a first-class citizen (also type, object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, modified, and assigned to a variable.

大意是說,在編程語言中,所謂一等公民,是指支持所有操作的實體, 這些操作通常包括作為參數(shù)傳遞,從函數(shù)返回,修改并分配給變量等。

比如 int 類型,它支持作為參數(shù)傳遞,可以從函數(shù)返回,也可以賦值給變量,因此它是一等公民。

類似的,函數(shù)是一等公民,意味著可以把函數(shù)賦值給變量或存儲在數(shù)據(jù)結(jié)構(gòu)中,也可以把函數(shù)作為其它函數(shù)的參數(shù)或者返回值。關(guān)于函數(shù)是一等公民,在維基百科也有定義[2]。

In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures. Some programming language theorists require support for anonymous functions (function literals) as well.In languages with first-class functions, the names of functions do not have any special status; they are treated like ordinary variables with a function type. The term was coined by Christopher Strachey in the context of "functions as first-class citizens" in the mid-1960s.

函數(shù)作為一等公民的概念是 1960 年由英國計算機學(xué)家 Christopher Strachey[3] 提出來的。然而,并非所有語言都將函數(shù)作為一等公民,特別是早期,比如 C 語言中函數(shù)就不是一等公民,一些功能通過函數(shù)指針來實現(xiàn)的;再比如 C++、Java 等,都是后來的版本才加上的。

一般來說,函數(shù)式編程語言、動態(tài)語言和現(xiàn)代的編程語言,函數(shù)都會作為一等公民,比如:Scala、Julia 等函數(shù)式語言,JavaScript、Python 等動態(tài)語言,Go、Rust、Swift 等現(xiàn)代的編譯型語言。

為了讓大家對函數(shù)是一等公民有更深的理解,針對上文提到的一等公民的一等功能,我們看看 Go 語言是如何支持的。

匿名函數(shù)

函數(shù)一般是有名字的,但有時候沒有名字的函數(shù)更簡潔、好用。沒有名字的函數(shù)叫匿名函數(shù)。

以下是 Go 語言匿名函數(shù)的一個例子:

  1. package main 
  2.  
  3. import ( 
  4.  "fmt" 
  5.  
  6. func main() { 
  7.  fn := func() { 
  8.   fmt.Println("This is anonymous function!"
  9.  } 
  10.  fn() 
  11.  
  12.  fmt.Printf("The type of fn: %T\n", fn) 
  13.  
  14. // output
  15. // This is anonymous function
  16. // The type of fn: func() 

在線運行:https://play.studygolang.com/p/IcInzZsAr0a。

在 Go 中,匿名函數(shù)最常使用的場景是開啟一個 goroutine,經(jīng)常會見到類似這樣的代碼:

  1. go func() { 
  2.   // xxxx 
  3. }() 

這里匿名函數(shù)定義后立即調(diào)用。此外,defer 語句中也常見。

定義函數(shù)類型

定義函數(shù)類型和其他類型類似,同時后半部分和匿名函數(shù)類似,只不過沒有函數(shù)實現(xiàn)。比如 net/http 包中的 HandlerFunc 函數(shù)類型:

  1. type HandlerFunc func(ResponseWriter, *Request) 

怎么使用這個類型?能看懂這樣的代碼,表示你理解了:

  1. var h http.HandlerFunc = func(w ResponseWriter, req *Request) { 
  2.   fmt.Fprintln(w, "Hello World!"

函數(shù)作為參數(shù)

意思是說,一個函數(shù)作為另一個函數(shù)的參數(shù),也就是回調(diào),在 JS 中很常見。在 Go 語言中也經(jīng)常出現(xiàn)。文章開頭的問題就是函數(shù)作為參數(shù)。根據(jù) Gin 的 API 定義,router.GET 方法的簽名如下:

  1. func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) IRoutes 

其中 HandlerFunc 是一個函數(shù)類型,它的定義如下:

  1. type HandlerFunc func(*Context) 

所以,router.GET("/users", Users) 中,Users 只是 GET 函數(shù)的參數(shù),參數(shù)類型是 HandlerFunc,而 Users 的定義只要符合 HandlerFunc 即可:

  1. func Users(ctx *gin.Context) {} 

因為這里將函數(shù) Users 作為參數(shù),所以自然不需要給 Users 傳遞參數(shù),Uers 的調(diào)用有 GET 內(nèi)部負責(zé),即所謂的回調(diào)。

函數(shù)作為返回值

函數(shù)作為返回值,在 Go 中,這樣的函數(shù)一定是匿名函數(shù)。在進行 Web 開發(fā)時,中間件就會使用上函數(shù)作為返回值,還是以 Gin 為例,定義一個 Logger 中間件:

  1. func Logger() gin.HandlerFunc { 
  2.  return func(c *gin.Context) { 
  3.   t := time.Now() 
  4.  
  5.   // Set example variable 
  6.   c.Set("example""12345"
  7.  
  8.   // before request 
  9.  
  10.   c.Next() 
  11.  
  12.   // after request 
  13.   latency := time.Since(t) 
  14.   log.Print(latency) 
  15.  
  16.   // access the status we are sending 
  17.   status := c.Writer.Status() 
  18.   log.Println(status) 
  19.  } 

從上文知道,gin.HandlerFunc 是一個函數(shù)類型,因此需要返回一個該類型的實例,而匿名函數(shù)(函數(shù)字面值)只要和 gin.HandlerFunc 類型的底層類型一致,會進行隱式轉(zhuǎn)換,所以可以直接返回 func(c *gin.Context) {} 這個匿名類型。

經(jīng)常聽到高階函數(shù),函數(shù)是一等公民,就支持高階函數(shù)。一個函數(shù)只要接收一個或多個函數(shù)類型參數(shù);亦或是返回一個函數(shù),這樣的函數(shù)就叫做高階函數(shù)。

閉包

閉包(Closure)是匿名函數(shù)的一個特例。當(dāng)一個匿名函數(shù)所訪問的變量定義在函數(shù)體的外部時,就稱這樣的匿名函數(shù)為閉包。

一個簡單的例子:

  1. package main 
  2.  
  3. import (   
  4.     "fmt" 
  5.  
  6. func main() {   
  7.     a := 5 
  8.     func() { 
  9.         fmt.Println("a =", a) 
  10.     }() 

在上面的程序中,匿名函數(shù)在第 10 行訪問了變量 a,而 a 存在于函數(shù)體的外部。因此這個匿名函數(shù)就是閉包。

總結(jié)

以上的知識點,可以說是學(xué)習(xí)現(xiàn)代編程語言必須會的。如果你還有哪個點不明白,歡迎留言交流。

最后說明一點,Go 是不支持命名函數(shù)內(nèi)嵌的。即類似 JavaScript 中這樣的語法,Go 不支持:

  1. function outer() { 
  2.   console.log("In outer function"); 
  3.    
  4.   function inner() { 
  5.     console.log("In inner function"); 
  6.   } 

Go 只能通過匿名函數(shù)來實現(xiàn)。

參考資料

[1]一等公民: https://en.wikipedia.org/wiki/First-class_citizen

[2]維基百科也有定義: https://en.wikipedia.org/wiki/First-class_function

[3]Christopher Strachey: https://en.wikipedia.org/wiki/Christopher_Strachey

 本文轉(zhuǎn)載自微信公眾號「polarisxu」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系polarisxu公眾號。

 

責(zé)任編輯:武曉燕 來源: polarisxu
相關(guān)推薦

2022-03-27 23:11:39

Go語言函數(shù)

2023-03-28 07:26:37

2021-11-03 07:58:27

異步編程線程

2021-03-18 08:54:55

Go 語言函數(shù)

2011-08-31 13:12:36

2015-04-27 09:48:46

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

2022-12-08 08:40:25

大數(shù)據(jù)Hadoop存儲

2014-06-25 09:11:48

技術(shù)

2021-01-21 10:28:16

自然語言NLP人工智能

2022-09-29 09:22:33

數(shù)據(jù)倉

2022-11-07 18:12:54

Go語言函數(shù)

2019-10-23 19:30:23

AI 數(shù)據(jù)人工智能

2016-04-05 10:21:25

大數(shù)據(jù)元數(shù)據(jù)數(shù)據(jù)分析

2019-07-17 10:10:34

Netty版本Event

2023-06-11 17:02:24

數(shù)字化轉(zhuǎn)型數(shù)字經(jīng)濟

2021-04-26 22:19:57

計算

2024-08-26 08:36:26

2020-09-08 17:47:36

人工智能自然語言處理

2010-04-15 14:15:17

無線微蜂窩覆蓋

2020-03-09 16:43:06

腳本語言瀏覽器JavaScript
點贊
收藏

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