入门:基础语法(二)常量、枚举

栏目: Go · 发布时间: 8年前

内容简介:回顾一下,变量使用var关键字定义,常量就是用const关键字定义,特别注意,一定要有const关键字才能定义常量const的数值可以当作数值使用

golang的常量使用关键字const来定义,可以是文件内部的,也可以是函数内部的

回顾一下,变量使用var关键字定义,常量就是用const关键字定义,特别注意,一定要有const关键字才能定义常量

const (
	goldenPoint float64 = 0.618
	dota        string  = "TI"
)

func consts() {
	const pi float64 = 3.14
	fmt.Println(pi)
	fmt.Println(goldenPoint, dota)
}

const的数值可以当作数值使用

golang没有特别的枚举类型,但是提供了一个iota关键字来定义枚举的数字,然后用一组const来定义

func enums() {
	const (
		one   = 1
		two   = 2
		three = 3
	)
	fmt.Println(one, two, three)

	fmt.Println("第二种方式进行枚举(iota):")
	const (
		b = 1 << (10 * iota)
		kb
		mb
		gb
		//tb
		// 如果我们不想要枚举中的某个值,我们直接用占位符_取代
		_
		pb
	)
	fmt.Println("kb = ", kb)
	fmt.Println("pb = ", pb)
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Algorithms to Live By

Algorithms to Live By

Brian Christian、Tom Griffiths / Henry Holt and Co. / 2016-4-19 / USD 30.00

A fascinating exploration of how insights from computer algorithms can be applied to our everyday lives, helping to solve common decision-making problems and illuminate the workings of the human mind ......一起来看看 《Algorithms to Live By》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具