Golang常量iota的使用实例

发布时间:2020-08-23 04:26:06 作者:_John_Tian_
来源:脚本之家 阅读:137

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

总结

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

推荐阅读:
  1. iota在golang中的应用
  2. Golang中如何使用常量与变量

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

go 常量 iota

上一篇:java 输入某年某月某日,判断这一天是这一年的第几天

下一篇:数据结构--泛型编程

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》