主頁 > 知識庫 > Golang常量iota的使用實例

Golang常量iota的使用實例

熱門標(biāo)簽:Linux服務(wù)器 網(wǎng)站排名優(yōu)化 鐵路電話系統(tǒng) 服務(wù)外包 呼叫中心市場需求 AI電銷 地方門戶網(wǎng)站 百度競價排名

Codes

package main
import "fmt"
type color byte
const (
  black color = iota
  red
  blue
)
func test(c color) {
  fmt.Println(c)
}
func main() {
  const (
    x = iota // 0
    y    // 1
    z    // 2
  )
  fmt.Printf("x=%v, y=%v, z=%v\n", x, y, z)
  const (
    _ = iota
    KB = 1  (10 * iota) // 1  (10 * 1)
    MB          // 1  (10 * 2)
    GB          // 1  (10 * 3)
  )
  fmt.Printf("KB=%v, MB=%v, GB=%v\n", KB, MB, GB)
  const (
    _, _  = iota, iota * 10 // 0, 0 * 10
    aa, bb          // 1, 1 * 10
    cc, dd          // 2, 2 * 10
  )
  fmt.Printf("aa=%v, bb=%v, cc=%v, dd=%v\n", aa, bb, cc, dd)
  const (
    a = iota // 0
    b    // 1
    c = 100 // 100
    d    // 100
    e = iota // 4
    f    // 5
  )
  fmt.Printf("a=%v, b=%v, c=%v, d=%v, e=%v, f=%v\n", a, b, c, d, e, f)
  const (
    g     = iota // 0
    h float32 = iota // 1
    i     = iota // 2
  )
  fmt.Printf("g: %T %v, f: %T %v, h: %T %v\n", g, g, h, h, i, i)
  test(black) // 0
  test(red)  // 1
  test(blue) // 2
  test(100)  // 100 并未超出 color/byte 類型取值范圍
  // xx := 2
  // test(xx)
}

Result

x=0, y=1, z=2
KB=1024, MB=1048576, GB=1073741824
aa=1, bb=10, cc=2, dd=20
a=0, b=1, c=100, d=100, e=4, f=5
g: int 0, f: float32 1, h: int 2
0
1
2
100

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

您可能感興趣的文章:
  • 詳解Golang編程中的常量與變量
  • Golang學(xué)習(xí)筆記(二):類型、變量、常量
  • 手把手帶你走進Go語言之常量解析

標(biāo)簽:銅川 黃山 仙桃 崇左 湖南 衡水 湘潭 蘭州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Golang常量iota的使用實例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266