主页 > 知识库 > Golang常量iota的使用实例

Golang常量iota的使用实例

热门标签:Linux服务器 网站排名优化 铁路电话系统 服务外包 呼叫中心市场需求 AI电销 地方门户网站 百度竞价排名

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

您可能感兴趣的文章:
  • 详解Golang编程中的常量与变量
  • Golang学习笔记(二):类型、变量、常量
  • 手把手带你走进Go语言之常量解析

标签:铜川 黄山 仙桃 崇左 湖南 衡水 湘潭 兰州

巨人网络通讯声明:本文标题《Golang常量iota的使用实例》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266