常量用[color=red][b]const[/b][/color]关键字定义,用于存储不会改变的数据
常量的数据类型一般是bool,数字和字符串
定义的方式:
[quote]const {name} {type} = {value} 或 const {name} = value[/quote]
也可以多个定义
[quote]const {name1},{name2} {type} = {value1},{value2}[/quote]
用列子说明:
结果:
[img]http://dl2.iteye.com/upload/attachment/0126/6245/46e6ecf3-8e4a-39cd-bb01-e1544824724b.png[/img]
注意:iota是golang语言的常量计数器,只能在常量的表达式中使用
iota在const关键字出现时将被重置为0(const内部的第一行之前)
reflect包是go的反射包,reflect.TypeOf反射数据类型
常量的数据类型一般是bool,数字和字符串
定义的方式:
[quote]const {name} {type} = {value} 或 const {name} = value[/quote]
也可以多个定义
[quote]const {name1},{name2} {type} = {value1},{value2}[/quote]
用列子说明:
package main
import (
"fmt"
"reflect"
)
const cK string = "string const"
const cj = "string j"
const (
a int = 1
b
c int = iota
d
)
const (
e, f, g = 5, "6", 7
)
func main() {
fmt.Printf("cK:%s,cj:%s,a:%d,b:%d,c:%d,d:%d,e:%d,f:%s,g:%d\n", cK, cj, a, b, c, d, e, f, g)
fmt.Println(reflect.TypeOf(f))
}
结果:
[img]http://dl2.iteye.com/upload/attachment/0126/6245/46e6ecf3-8e4a-39cd-bb01-e1544824724b.png[/img]
注意:iota是golang语言的常量计数器,只能在常量的表达式中使用
iota在const关键字出现时将被重置为0(const内部的第一行之前)
reflect包是go的反射包,reflect.TypeOf反射数据类型