新聞中心
go 語言枚舉類型是這么用的?在什么場(chǎng)景下會(huì)用到枚舉?本文對(duì) go 語言枚舉做了詳細(xì)講解。

創(chuàng)新互聯(lián)主要從事做網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)岱山,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
枚舉,是一種重要的數(shù)據(jù)類型,由一組鍵值對(duì)組成,通常用來在編程語言中充當(dāng)常量的標(biāo)識(shí)符。在主流行編程語言如 c、 java 等,都有原生支持。在 go 中,大家卻找不到 enum 或者其它直接用來聲明枚舉類型的關(guān)鍵字。從熟悉其它編程語言的開發(fā)者轉(zhuǎn)用 go 編程,剛開始會(huì)比較難接受這種情況。其實(shí),如果你看到如何在 go 中表示枚舉類型時(shí),可能會(huì)感受到 go 語言設(shè)計(jì)者對(duì)簡(jiǎn)潔性、問題考慮的深度,是一般資淺工程師無法比擬的。
其實(shí),在 go 語言設(shè)計(jì)者的眼里,enum 本質(zhì)是常量,為什么要多余一個(gè)關(guān)鍵字呢?在 go 只是沒有 enum 關(guān)鍵字而已,其表現(xiàn)枚舉的形式,與其它語言別無太大區(qū)別。下面來看看如果在 go 中表示枚舉。
學(xué)習(xí)、使用一門語言,是學(xué)習(xí)、理解語言本身的設(shè)計(jì)哲學(xué),同時(shí)也會(huì)感受到設(shè)計(jì)者的性格特點(diǎn)。
基礎(chǔ)工作
為了下面講解方便,這里使用 go modules 的方式先建立一個(gè)簡(jiǎn)單工程。
- ~/Projects/go/examples
- mkdir enum
- ~/Projects/go/examples
- cd enum
- ~/Projects/go/examples/enum
- go mod init enum
- go: creating new go.mod: module enum
- ~/Projects/go/examples/enum
- touch enum.go
const + iota
以 啟動(dòng)、運(yùn)行中、停止 這三個(gè)狀態(tài)為例,使用 const 關(guān)鍵來聲明一系列的常量值。在 enum.go 中寫上以下內(nèi)容:
- package main
- import "fmt"
- const (
- Running int = iota
- Pending
- Stopped
- )
- func main() {
- fmt.Println("State running: ", Running)
- fmt.Println("State pending: ", Pending)
- fmt.Println("State Stoped: ", Stopped)
- }
保存并運(yùn)行,可以得到以下結(jié)果,
- ~/Projects/go/examples/enum
- go run enum.go
- State running: 0
- State pending: 1
- State Stoped: 2
在說明發(fā)生了什么之前,我們先看來一件東西,iota。相比于 c、java,go 中提供了一個(gè)常量計(jì)數(shù)器,iota,它使用在聲明常量時(shí)為常量連續(xù)賦值。
比如這個(gè)例子,
- const (
- a int = iota // a = 0
- b int = iota // b = 1
- c int = iota // c = 2
- )
- const d int = iota // d = 0
在一個(gè) const 聲明塊中,iota 的初始值為 0,每聲明一個(gè)變量,自增 1。以上的代碼可以簡(jiǎn)化成:
- const (
- a int = iota // a = 0
- b // b = 1
- c // c = 2
- )
- const d int = iota // d = 0
設(shè)想一下,如果此時(shí)有 50 或者 100 個(gè)常量數(shù),在 c 和 java 語言中寫出來會(huì)是什么情況。
關(guān)于 iota,有更多的具體的技巧(例如跳數(shù)),詳細(xì)請(qǐng)看官方定義 iota。
通過使用 const 來定義一連串的常量,并借助 iota 常量計(jì)數(shù)器,來快速的為數(shù)值類型的常量連續(xù)賦值,非常方便。雖然沒有了 enum 關(guān)鍵字,在這種情況下發(fā)現(xiàn),是多余的,枚舉本質(zhì)上就是常量的組合。
當(dāng)然,你可以使用以下方式,來更接近其它語言的 enum,
- // enum.go
- ...
- type State int
- const (
- Running State = iota
- Pending
- Stopped
- )
- ...
把一組常量值,使用一個(gè)類型別名包裹起來,是不是更像其它語言中的 enum {} 定義了呢?
你還可以將上面的例子改為:
- // enum.go
- ...
- type State int
- const (
- Running State = iota
- Pending
- Stopped
- )
- func (s State) String() string {
- switch s {
- case Running:
- return "Running"
- case Pending:
- return "Pending"
- case Stopped:
- return "Stopped"
- default:
- return "Unknown"
- }
- }
- ...
為定義的枚舉類型加上 String 函數(shù),運(yùn)行結(jié)果如下:
- ~/Projects/go/examples/enum
- go run enum.go
- State running: Running
- State pending: Pending
- State Stoped: Stopped
是不是很魔幻,思路一下又開闊一些,長(zhǎng)見識(shí)了。把實(shí)際的值與打印字符分開,一般語言設(shè)計(jì)者不會(huì)想到??吹竭@里,有沒有這種的感覺,go 語言的設(shè)計(jì)者并不是偷懶,而是為了可以偷懶想了很多、做了很多。
名稱欄目:Golang如何表示枚舉類型
轉(zhuǎn)載來源:http://m.fisionsoft.com.cn/article/cociged.html


咨詢
建站咨詢
