ITeye博客

go语言常量的使用

studypi · 2017-08-21 10:34 · go · 编程语言 · Markdown

常量用const关键字定义,用于存储不会改变的数据 常量的数据类型一般是bool,数字和字符串 定义的方式:

const {name} {type} = {value} 或 const {name} = value

也可以多个定义

const {name1},{name2} {type} = {value1},{value2}

用列子说明:


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))
}

结果:

注意:iota是golang语言的常量计数器,只能在常量的表达式中使用 iota在const关键字出现时将被重置为0(const内部的第一行之前) reflect包是go的反射包,reflect.TypeOf反射数据类型

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。