一、常量的定义
- const filename = "abc.txt"
- const 数值可以作为任何类型使用
- const a, b = 3, 4
- var c int = int(math.Sqrt(3*3 + 4*4))
二、iota
- iota,特殊常量,可以认为是一个可以被编译器修改的常量。
- iota 在 const关键字出现时将被重置为 0(const 内部的第一行之前),const 中每新增一行常量声明将使 iota 计数一次(iota 可理解为 const 语句块中的行索引)。
- iota 可以被用作枚举值:
三、例子
package main
import (
"fmt"
"math"
)
// 常量定义
func consts() {
const filename = "abc.txt"
const a,b = 4,5
var c int
c = int(math.Sqrt(a*a + b*b))
fmt.Println(filename,c)
}
// 枚举
func enums () {
// 普通枚举类型
const (
java = 1
c = 2
python = 3
golang = 4
)
fmt.Println(java,c,python,golang)
// 自增枚举
const (
lua = iota
_
shell
perl
css
js
)
fmt.Println(lua,shell,perl,css,js)
// k,kb,mb,gb.tb,pb,eb
const (
b = 1 << (10*iota)
kb
mb
gb
pb
eb
)
fmt.Println(b,kb,mb,gb,pb,eb)
}
func main ( ) {
fmt.Println("hello world!")
consts()
enums()
}