Go-常量-const

常量:只能读,不能修改,编译前就是确定的值

关键字: const

常量相关类型:int8,16,32,64 float32,64 bool string 可计算结果数学表达式

常量方法  iota

package main

import "fmt"

func main() {
	const name  = "BeinMenChuiXue"
	fmt.Println(name)

	const (
		homeAddr = "earth"
		nickName = "WeiLaiShiXuePai"
	)
	fmt.Println(homeAddr, nickName)

	const (
		zero = iota
		one
		two = iota
		three
	)
	fmt.Println(zero, one, two, three)

	const (
		first = iota
		second
	)
	fmt.Println(first, second)

	const (
		student = "Golang"
		teacher
		parents = "S"
		children
	)
	fmt.Println(student, teacher, parents, children)
}

  

观察输出结果,得出以下有关常量特性

  1. 定义多个常量推荐使用括号 

  2. iota每往下一行自增1并赋值给这一行的常量,遇到下一个const重新从0开始

  3. 常量未赋值,往上找最近有值的常量并赋值给自己

猜你喜欢

转载自www.cnblogs.com/2bjiujiu/p/9029523.html
今日推荐