golang 常量與列舉

kenuo發表於2018-05-11
package main

import "fmt"

func main() {
    const (
        cpp = iota
        java
        python
        php
        javascript
    )

    fmt.Println(cpp,javascript,java,python,php)
}

file

  • 如果我們不想要某個值咋寫呢?
package main

import "fmt"

func main() {
    const (
        cpp = iota
        _
        python
        php
        javascript
    )

    fmt.Println(cpp,javascript,python,php)
}

file

  • 這樣 Java 就會被跳過了
package main

import "fmt"

func main() {

    //b , kb , mb,gb ,tb,pb
    const (
        b = 1 << (10 * iota)
        kb
        mb
        gb
        tb
        pb
    )

    fmt.Println(b , kb , mb,gb ,tb,pb)
}

file

by JeffreyBool blog :point_right: link

相關文章