iota的确切含义是什么?

在下面的代码中:


const (

    signature uint32 = 0xae3179fb

    dhkxGroup = 2


    ReplySuccessful byte = iota

    ReplyBufferCorrupted

    ReplyDecryptFailed

    ReplySessionExpired

    ReplyPending

)

ReplySuccessful编译为 ,而我认为它肯定应该是零。如果我移动并低于 ,则变为 0。2signaturedhkxGroupReplyPendingReplySuccessful


这是为什么呢?


PS.对我来说,使用iota的唯一“好处”是您可以省略分配给以后常量的值,以便您可以轻松修改/插入新值。但是,如果iota没有固定为零,则可能会导致大问题,尤其是在执行通信协议等操作时。


摇曳的蔷薇
浏览 141回答 1
1回答

郎朗坤

该规范定义了iota在Go中的用法(着重号是后加的):在常量声明中,预声明的标识符 iota 表示连续的非类型化整数常量。它的值是该常量声明中相应&nbsp;ConstSpec&nbsp;的索引,从零开始。请注意,索引是相对于 的,基本上表示当前块。ConstSpecconst特别令人感兴趣的可能是提供的示例:const (&nbsp; a = 1 << iota&nbsp; // a == 1&nbsp; (iota == 0)&nbsp; b = 1 << iota&nbsp; // b == 2&nbsp; (iota == 1)&nbsp; c = 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // c == 3&nbsp; (iota == 2, unused)&nbsp; d = 1 << iota&nbsp; // d == 8&nbsp; (iota == 3))请注意,第 3 行(值 2)未使用。您基本上具有相同的,首先是两个未使用的值。您在代码中可能意味着:const (&nbsp; &nbsp; signature uint32 = 0xae3179fb&nbsp; &nbsp; dhkxGroup = 2)const (&nbsp; &nbsp; ReplySuccessful byte = iota&nbsp; &nbsp; ReplyBufferCorrupted&nbsp; &nbsp; ReplyDecryptFailed&nbsp; &nbsp; ReplySessionExpired&nbsp; &nbsp; ReplyPending)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go