当前位置:网站首页>Go语言中的Iota关键字怎么使用
Go语言中的Iota关键字怎么使用
2022-07-17 00:56:00 【亿速云】
Go语言中的Iota关键字怎么使用
这篇文章主要讲解了“Go语言中的Iota关键字怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Go语言中的Iota关键字怎么使用”吧!
一、复习常量
提到Iota这个关键字,就必须要复习一下Go语言的常量。
1.Go语言的常量一般使用const声明
2.Go语言的常量只能是布尔型、数字型(整数型、浮点型和复数)和字符串型
3.Go语言的常量可以不指定类型,由编译器自己推断,如下面的【string】就是可以不写的(也称为 隐式类型定义)
const s string = "constant"
4.常量不能在程序运行时改变
二、Iota的用法
通过一个具体的事例,来看iota的特性。例下面的代码输出的结果每一个常量的值是什么?
package IotaDemoimport ( "fmt")const( a = iota b c)const( m=1<<iota n=2<<iota x=10 y = iota z=iota>>1 o)func IotaTest() { fmt.Println("a=",a) fmt.Println("b=",b) fmt.Println("c=",c) fmt.Println("m=",m) fmt.Println("n=",n) fmt.Println("x=",x) fmt.Println("y=",y) fmt.Println("z=",z) fmt.Println("o=",o)}结果如下:
a= 0
b= 1
c= 2
m= 1
n= 4
x= 10
y= 3
z= 2
o= 2
上面的结果说明了iota的特性,如下:
iota只能被使用在const限定中,相当于一个常量的计数器
iota相当于一个枚举值,默认从0开始,在一个const中,会进行+1,例如a、b、c 可以看出来
iota不会因为const中被赋值了固定值,就不再增加,const中每有一个常量就+1,例如 x和y ,y并没有因为x被赋值为10,就是2,而是3
每次进入一个新的const,iota都会重新开始计算
补充移位操作知识点:
对于Go语言的<<(右移)和 >>(左移)操作,关键点如下:
1.将移位的值先转换成2进制,右移动就是高位补0,左移就是低位补0
2.操作符右边的是需要移动的位数,左边是待移动的值,
例如 1<<3 就是把1向左移动3位,即00000001 左移3位 变为 00000100 就是4
3<<1 就是把3向左移动1为,即00000011 左移1为 变为 00000110 就是6
感谢各位的阅读,以上就是“Go语言中的Iota关键字怎么使用”的内容了,经过本文的学习后,相信大家对Go语言中的Iota关键字怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
边栏推荐
- [regression prediction] lithium ion battery life prediction based on particle filter with matlab code
- [MCU simulation] (XVIII) control transfer instructions - empty operation instructions
- Wechat applet
- Use RZ, SZ commands to upload and download files through xshell7
- [single chip microcomputer simulation] (XI) instruction system logic operation instruction - logic and instruction anl, logic or instruction ORL
- The installation software prompts that the program input point adddlldirectory cannot be located in the dynamic link library kernel32 DLL (download address at the end of the text)
- Vs code problem: launch:program '... \ vscode\launch. exe‘ dose not exist
- Envi: (the most detailed tutorial in 2022) custom coordinate system
- ncnn DataReader&Extractor&blob
- While loop
猜你喜欢

Chengxin University envi_ The second week of IDL experiment content: extract aod+ in all MODIS aerosol products for detailed analysis

Shell script receives and returns parameters

Configure high availability using virtual ip+kept
![Dqn theoretical basis and code implementation [pytoch + cartpole-v0]](/img/cf/32438e403544aa42e2fdd2e181327c.png)
Dqn theoretical basis and code implementation [pytoch + cartpole-v0]

Flutter development: running the flutter upgrade command reports an error exception:flutter failed to create a directory at... Solution

05_ Service call ribbon

Polynomial interpolation fitting (III)
![[MySQL] MHA high availability](/img/d3/d9830f3c331193fd40b8f00ebe35fa.png)
[MySQL] MHA high availability
![深入理解机器学习——类别不平衡学习(Imbalanced Learning):样本采样技术-[人工采样技术之SMOTE采样法及Borderline-SMOTE采样法]](/img/9f/a0d03b23e66849f12150f9a72f36c5.png)
深入理解机器学习——类别不平衡学习(Imbalanced Learning):样本采样技术-[人工采样技术之SMOTE采样法及Borderline-SMOTE采样法]

2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
随机推荐
04_服务注册Eureka
Gdb+vscode for debugging 8 - use core to analyze dead cycles, deadlocks, and segment errors
Can't access this website can't find DNS address DNS_ PROBE_ What about started?
It's good to take more exercise
[MCU simulation] (VII) addressing mode - bit addressing
zsh: command not found: mysql
05 central processing unit
Bisenetv1 face segmentation
Dqn theoretical basis and code implementation [pytoch + cartpole-v0]
[MySQL] data query operation (select statement)
Has DDD surpassed MVC
【模板记录】字符串哈希判断回文串
Es6 notes d'étude - station B Xiao Ma Ge
[NoSQL] redis high availability and persistence
Fiddler grabbing
Polynomial interpolation fitting (I)
[MCU simulation] (V) addressing mode - immediate addressing and register indirect addressing
Comparison between redis and other databases
Yolov5 opencv DNN reasoning
MySQL interview questions (2022)