Go語(yǔ)言中的iota關(guān)鍵字的使用
基本使用
示例
package main import "fmt" const a0 = iota // a0 = 0 // const出現(xiàn), iota初始化為0 const ( a1 = iota // a1 = 0 // 又一個(gè)const出現(xiàn), iota初始化為0 a2 = iota // a2 = 1 // const新增一行, iota 加1 a3 = 6 // a3 = 6 // 自定義一個(gè)常量 a4 // a4 = 6 // 不賦值就和上一行相同 a5 = iota // a5 = 4 // const已經(jīng)新增了4行, 所以這里是4 ) const ( b = iota // b = 0 c // c = 1 d = 1111 e f = iota ) const ( TestMin = -1 TestA TestB = iota TestC ) func main() { fmt.Println("a0:", a0) fmt.Println("a1:", a1) fmt.Println("a2:", a2) fmt.Println("a3:", a3) fmt.Println("a4:", a4) fmt.Println("a5:", a5) fmt.Println("---------") fmt.Println("b is:", b) fmt.Println("c is:", c) fmt.Println("d is:", d) fmt.Println("e is:", e) fmt.Println("f is:", f) fmt.Println("---------") fmt.Println("TestMin:", TestMin) fmt.Println("TestA:", TestA) fmt.Println("TestB:", TestB) fmt.Println("TestC:", TestC) }
輸出為:
a0: 0
a1: 0
a2: 1
a3: 6
a4: 6
a5: 4
---------
b is: 0
c is: 1
d is: 1111
e is: 1111
f is: 4
---------
TestMin: -1
TestA: -1
TestB: 2
TestC: 3
示例
package main import "fmt" // 跳值使用iota使用_來(lái)達(dá)到目的: const ( h = iota // 0 i = iota // 1 _ k = iota // 3 ) // 當(dāng)常量表達(dá)式為空時(shí),會(huì)自動(dòng)繼承上一個(gè)存在非空的表達(dá)式。 const ( x = iota * 2 y z ) func main() { fmt.Println(h) fmt.Println(i) fmt.Println(k) fmt.Println("-------") fmt.Println(x) fmt.Println(y) fmt.Println(z) }
輸出為
0
1
3
-------
0
2
4
用來(lái)定義枚舉值
package main import "fmt" type OrderState uint8 const ( // 0-待支付 WaitPay OrderState = iota // 1-支付中 Paying // 2-支付成功 PaySucc // 3-支付失敗 PayFail ) // 對(duì)應(yīng)于xx表is_show字段 const ( _ uint8 = iota Show // 1-顯示 NotShow // 2-不顯示 ) func main() { fmt.Println(PaySucc) fmt.Println(Show) }
輸出為
2
1
高階用法
package main import ( "fmt" ) const ( i = 1 << iota j = 3 << iota k l ) func main() { fmt.Println("i=", i) fmt.Println("j=", j) fmt.Println("k=", k) fmt.Println("l=", l) }
輸出為
i= 1
j= 6
k= 12
l= 24
iota每出現(xiàn)一次,自動(dòng)加1;而前面的操作數(shù)如果不指定,默認(rèn)使用上一個(gè)的,在這里是3;
即
i=1<<iota
j=3<<iota
k
l
等價(jià)于
i=1<<0
j=3<<1
k=3<<2
l=3<<3
又如
package main import "fmt" func main() { const ( IgEggs = 1 << iota // 1 << 0 which is 00000001 IgChocolate // 1 << 1 which is 00000010 IgNuts // 1 << 2 which is 00000100 IgStrawberries // 1 << 3 which is 00001000 IgShellfish // 1 << 4 which is 00010000 ) fmt.Println(IgEggs, IgChocolate, IgNuts, IgStrawberries, IgShellfish) }
輸出為
1 2 4 8 16
每次可以左移一位,因此對(duì)于定義數(shù)量級(jí)大有裨益
package main import "fmt" type ByteSize int64 const ( _ = iota // ignore first value by assigning to blank identifier KB ByteSize = 1 << (10 * iota) // 1 << (10*1) MB // 1 << (10*2) GB // 1 << (10*3) TB // 1 << (10*4) PB // 1 << (10*5) EB // 1 << (10*6) //ZB // 1 << (10*7) //YB // 1 << (10*8) ) func main() { fmt.Printf("KB= %d Byte\n", KB) fmt.Printf("MB= %d Byte\n", MB) fmt.Printf("GB= %d Byte\n", GB) }
輸出為
KB= 1024 Byte
MB= 1048576 Byte
GB= 1073741824 Byte
以上就是Go語(yǔ)言中的iota關(guān)鍵字的使用的詳細(xì)內(nèi)容,更多關(guān)于Go iota的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
go?zero微服務(wù)實(shí)戰(zhàn)性能優(yōu)化極致秒殺
這篇文章主要為大家介紹了go-zero微服務(wù)實(shí)戰(zhàn)性能優(yōu)化極致秒殺功能實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07GoLang中Json?Tag用法實(shí)例總結(jié)
這篇文章主要給大家介紹了關(guān)于GoLang中Json?Tag用法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02Golang時(shí)間處理庫(kù)go-carbon?v2.2.13發(fā)布細(xì)則
這篇文章主要為大家介紹了Golang?時(shí)間處理庫(kù)go-carbon?v2.2.13發(fā)布細(xì)則,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Go語(yǔ)言對(duì)字符串進(jìn)行SHA1哈希運(yùn)算的方法
這篇文章主要介紹了Go語(yǔ)言對(duì)字符串進(jìn)行SHA1哈希運(yùn)算的方法,實(shí)例分析了Go語(yǔ)言針對(duì)字符串操作的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03一文帶你學(xué)會(huì)Go?select語(yǔ)句輕松實(shí)現(xiàn)高效并發(fā)
這篇文章主要為大家詳細(xì)介紹了Golang中select語(yǔ)句的用法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Golang有一定的幫助,需要的可以參考一下2023-03-03Golang源碼分析之golang/sync之singleflight
golang/sync庫(kù)拓展了官方自帶的sync庫(kù),提供了errgroup、semaphore、singleflight及syncmap四個(gè)包,本次先分析第一個(gè)包errgroup的源代碼,下面這篇文章主要給大家介紹了關(guān)于Golang源碼分析之golang/sync之singleflight的相關(guān)資料,需要的朋友可以參考下2022-11-11