Go常量
2024-10-20 18:08:56
1. 常量
package main import "fmt" func main() {
/*
常量:
1.概念:同变量类似,程序执行过程中数值不能改变
2.语法:
显式类型定义: const b string = "abc"
隐式类型定义: const b = "abc" 3.常数:
固定的数值:100,"abc"
*/
fmt.Println(100)
fmt.Println("hello") //1.定义常量
const PATH string = "http:www.baidu.com"
const PI = 3.14
fmt.Println(PATH)
//fmt.Println(PI) //2.尝试修改常量的数值
//PATH = "http://www.sina.com" //cannot assign to PATH //3.定义一组常量
const C1, C2, C3 = 100, 3.14, "haha"
const (
MALE = 0
FEMALE = 1
UNKNOW = 3
)
//4.一组常量中,如果某个常量没有初始值,默认和上一行一致
const (
a int = 100
b
c string = "ruby"
d
e
)
fmt.Printf("%T,%d\n", a, a)
fmt.Printf("%T,%d\n", b, b)
fmt.Printf("%T,%s\n", c, c)
fmt.Printf("%T,%s\n", d, d)
fmt.Printf("%T,%s\n", e, e) //5. 枚举类型:使用常量组作为枚举类型。一组相关数值的数据
const (
SPRING = 0
SUMMER = 1
AUTUMN = 2
WINTER = 3
) // 注意:
// 常量中的数据类型只可以是布尔型、数字型(整数型、浮点型和复数)和字符串型
// 不曾使用的常量,在编译的时候,是不会报错的
// 显示指定类型的时候,必须确保常量左右值类型一致,需要时可做显示类型转换。这与变量就不一样了,变量是可以是不同的类型值 }
2. iota
package main import (
"fmt"
) func main() {
/*
iota:特殊的常量,可以被编译器自动修改的常量
每当定义一个const,iota的初始值为0
每当定义一个常量,就会自动累加1
直到下一个const出现,清零
*/
const (
a = iota // 0
b = iota // 1
c = iota //2
)
fmt.Println(a)
fmt.Println(b)
fmt.Println(c) const (
d = iota // 0
e // 1
)
fmt.Println(d)
fmt.Println(e) //枚举中
const (
MALE = iota // 0
FEMALE // 1
UNKNOW // 2
)
fmt.Println(MALE, FEMALE, UNKNOW) }
package main import "fmt" func main() {
const (
A = iota // 0
B // 1
C // 2
D = "haha" // iota = 3
E // haha iota = 4
F = 100 //iota =5
G //100 iota = 6
H = iota // 7
I //iota 8
)
const (
J = iota // 0
)
fmt.Println(A)
fmt.Println(B)
fmt.Println(C)
fmt.Println(D)
fmt.Println(E)
fmt.Println(F)
fmt.Println(G)
fmt.Println(H)
fmt.Println(I)
fmt.Println(J)
}
最新文章
- GitHub + VSTS 开源代码双向同步
- PyAutoGUI 简介
- 小型文件数据库 (a file database for small apps) SharpFileDB
- Struts2(3) —— 数据处理
- MyEclipse 2016 CI 3发布
- 您还在招聘网上海量投简历然后等面试机会吗?那你已经OUT了。
- How to apply Local Group Policy settings silently using the ImportRegPol.exe and Apply_LGPO_Delta.exe utilities.
- JSP中,当页面为404或者500时。设置跳转到错误提示页面
- [C++程序设计]用数组名作函数参数
- centos下添加的端口不能访问(防火墙关闭)
- 《JavaScript DOM 编程艺术》
- ipython安装( jupyter)
- CentOS7.5安装nodejs
- bzoj4129 Haruna’s Breakfast 莫队
- IDEA配置GIT
- 使用SDNN (space displacement neural network)进行多字体手写识别
- Effective C++ Item 34 Differentiate between inheritance of interface and inheritance of implementation
- LINQ操作List<;T>;
- mybatis生成的pojo 中的属性或方法不够我们当做dto使用时
- 转:PriorityQueue
热门文章
- [Agc005D/At2060] Minimum Sum - 单调栈
- solr 对于 关键字的特殊处理
- 安装python3.7.4时报错:Service Pack 1 is required to continue installation
- Oracle11g配置监听
- Java修饰符类型
- jQuery添加/删除元素
- [Luogu]中位数
- SQL语句分类和语法
- asmx 、Web Service、Web API
- SQL中 select count(1) count中的1 到底是什么意思呢?和count(*)的区别