

go IOTA常量计数器1期
source link: https://studygolang.com/articles/13623?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

iota是golang语言的常量计数器,只能在常量的表达式中使用。
iota在const关键字出现时将被重置为0(const内部的第一行之前),const中每新增一行常量声明将使iota计数一次(iota可理解为const语句块中的行索引)。
使用iota能简化定义,在定义枚举时很有用。
举例如下:
1、iota只能在常量的表达式中使用。
fmt.Println(iota)
编译错误: undefined: iota
2、每次 const 出现时,都会让 iota 初始化为0.
const a = iota // a=0 const ( b = iota //b=0 c //c=1 )
3、自定义类型
自增长常量经常包含一个自定义枚举类型,允许你依靠编译器完成自增设置。
type Stereotype int const ( TypicalNoob Stereotype = iota // 0 TypicalHipster // 1 TypicalUnixWizard // 2 TypicalStartupFounder // 3 )
4、可跳过的值
设想你在处理消费者的音频输出。音频可能无论什么都没有任何输出,或者它可能是单声道,立体声,或是环绕立体声的。
这可能有些潜在的逻辑定义没有任何输出为 0,单声道为 1,立体声为 2,值是由通道的数量提供。
所以你给 Dolby 5.1 环绕立体声什么值。
一方面,它有6个通道输出,但是另一方面,仅仅 5 个通道是全带宽通道(因此 5.1 称号 - 其中 .1 表示的是低频效果通道)。
不管怎样,我们不想简单的增加到 3。
我们可以使用下划线跳过不想要的值。
type AudioOutput int const ( OutMute AudioOutput = iota // 0 OutMono // 1 OutStereo // 2 _ _ OutSurround // 5 )
5、位掩码表达式
type Allergen int const ( IgEggs Allergen = 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 )
这个工作是因为当你在一个 const 组中仅仅有一个标示符在一行的时候,它将使用增长的 iota 取得前面的表达式并且再运用它,。在 Go 语言的 spec 中, 这就是所谓的隐性重复最后一个非空的表达式列表。
如果你对鸡蛋,巧克力和海鲜过敏,把这些 bits 翻转到 “on” 的位置(从左到右映射 bits)。然后你将得到一个 bit 值 00010011,它对应十进制的 19。
fmt.Println(IgEggs | IgChocolate | IgShellfish) // output: // 19
6、定义数量级
type ByteSize float64 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) )
7、定义在一行的情况
const ( Apple, Banana = iota + 1, iota + 2 Cherimoya, Durian Elderberry, Fig ) iota 在下一行增长,而不是立即取得它的引用。 // Apple: 1 // Banana: 2 // Cherimoya: 2 // Durian: 3 // Elderberry: 3 // Fig: 4
8、中间插队
const ( i = iota j = 3.14 k = iota l )
那么打印出来的结果是 i=0,j=3.14,k=2,l=3
9、二进制移动
const ( bit00 uint32 = 5 << iota //bit00=5 bit01 //bit01=10 bit02 //bit02=20 ) fmt.Printf("bit00 = %d, bit01 = %d, bit02 = %d\n", bit00, bit01, bit02) //bit00 = 5, bit01 = 10, bit02 = 20
Recommend
-
80
IOTA App WARNING: This wallet is now deprecated. Please use https://github.com/iotaledger/trinity-wallet instead. Prerequisites ...
-
33
-
45
今天,我们正式发布全新的状态客户端库的beta版本,其中包含我们称之为“帐户模块”的内容。这个名称不足以说明什么是重要的更新,因此我们将总结我此次更新客户端库的内容说明。 客户端库一直是构建IOTA上层应用的难题。库的功能...
-
30
快速一览 iota是Golang中提供的一个简化常量和枚举编程的标识符,合理的使用这个标识符可以让代码变得更简洁,省去大量的不必要的代码。 比如下面的这个常量定义 const ( a = 1 b = 2 c = 3 )...
-
12
3 tips for (slightly) better Go iota enums Go has no native enum types; the most idiomatic way of representing an enumerator is to use constants, many times along wi...
-
10
4 iota enum examples yourbasic.org/golang Iota basic example The
-
11
IOTA (MIOTA) – Hodlalert
-
15
Price$0.28544324 Hour % Change-0.76%Market Cap$793.40M
-
12
2021-01-01 20:54 IOTA基金会与Dark Matter Labs推出“Persistent Selv”演示 IOTA基金会与Dark Matter Labs合作推出“Persistent Selv”演示。该项目展示了自我主权身份(SSID)的继承和遗产。该项目演示由EIT Climat...
-
8
Looking for a Cryptocurrency That Isn’t Bitcoin? Ripple and IOTA May Be the Next Best Bet For the average investor, picking up some BTC to help pad out your wallet is great, but few have the time or...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK