美文网首页
golang const and itoa 2022-03-23

golang const and itoa 2022-03-23

作者: 9_SooHyun | 来源:发表于2022-03-23 11:28 被阅读0次

golang const

golang const定义常量时,如果出现省略的行,则自动沿用上一行的assignment

const (
        a = 1
        b = 5
        c
        d = 6
        e
        f
    )
fmt.Println(a, b, c, d, e, f)
// 1 5 5 6 6 6

golang itoa

  • 每次 const 出现时,都会让 iota本身 初始化为0。注意这句话的严谨性

  • const中每新增一行常量声明将使iota自增一次。同样注意这句话的严谨性

// 每次 const 出现时,都会让iota本身 初始化为0 + const中每新增一行常量声明将使iota自增一次
const (
        a = 1
        b = 5
        c = iota
        d = 6
        e
        f
    )
fmt.Println(a, b, c, d, e, f) 
// 1 5 2 6 6 6
-----
// const中【每新增一行常量声明】才使iota自增一次
const (
    Apple, Banana = iota + 1, iota + 2
    Cherimoya, Durian
    Elderberry, Fig
)

// Apple: 1 
// Banana: 2 
// Cherimoya: 2 
// Durian: 3 
// Elderberry: 3 
// Fig: 4

----
// 可以跳过部分值
type AudioOutput int

const ( 
    OutMute AudioOutput = iota // 0 
    OutMono                    // 1 
    OutStereo                  // 2 
    _ 
    _ 
    OutSurround                // 5 
)

----
// 中间插队
const(
    a = iota // 0
    b = 5    // 5
    c = iota  // 2
    d = 6  // 6
    e   // 6
    f //6
)


相关文章

网友评论

      本文标题:golang const and itoa 2022-03-23

      本文链接:https://www.haomeiwen.com/subject/qtecjrtx.html