码迷,mamicode.com
首页 > 其他好文 > 详细

Go常量与枚举类型

时间:2018-07-13 18:55:08      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:fun   mat   iot   name   ola   bsp   nbsp   ota   种类型   

 1 package main
 2 
 3 import (
 4     "math"
 5     "fmt"
 6 )
 7 
 8 //常量与枚举
 9 //const数值可作为各种类型使用
10 
11 func consts()  {
12     const fliename  = "abc.txt"  //常量名字不一定要全部大写
13     const a, b  = 3, 4  //不定义类型又可以int 又可以float,可以不用强制类型转换
14     var c int
15     c = int ( math.Sqrt( a * a + b * b))
16     fmt.Println( fliename, c)  //abc.txt 5
17 }
18 
19 func enums()  {
20     const(
21         cpp = 0
22         java = 1
23         python = 2
24         golang = 3
25     )
26     fmt.Println( cpp, java, python, golang)  //0 1 2 3
27 }
28 
29 func enums1()  {
30     const(
31         cpp = iota  //自增值
32         _
33         python
34         golang
35         javascript
36     )
37 
38     const(
39         b = 1 << ( 10 * iota)
40         kb
41         mb
42         gb
43         tb
44         pb
45     )
46     fmt.Println( cpp,  python, golang, javascript)  //0 2 3 4
47     fmt.Println(b, kb, mb, gb, tb, pb)  //1 1024 1048576 1073741824 1099511627776 1125899906842624
48 }
49 
50 func main() {
51     consts()
52     enums()
53     enums1()
54 }

 

Go常量与枚举类型

标签:fun   mat   iot   name   ola   bsp   nbsp   ota   种类型   

原文地址:https://www.cnblogs.com/yuxiaoba/p/9306486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!