当前位置:网站首页> 一篇文章学会GO语言中的变量
一篇文章学会GO语言中的变量
2022-07-04 13:48:00 【1024问】
1.标识符
2.关键字
3.变量
3.1 Go语言中变量的声明
3.2 批量声明
3.3 变量的初始化
3.4 短变量声明
3.5匿名变量
4.常量
5.iota
总结
1.标识符在编程语言中标识符就是程序员定义的具有特殊意义的词,比如变量名,常量名,函数 .bc,_123,a1232
2.关键字关键字是指编程语言中预先定义好的具有特殊含义的标识符,关键字和保留字都不建议用作变量名
Go语言中有25个关键字
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
还有37个保留字
3.变量Constants: true false iota nil
Types: int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
float32 float64 complex128 complex64
bool byte rune string errorFunctions: make len cap new append copy close delete
complex real imag
panic recover
变量的功能是存储数据,不同的变量保存的数据类型可能会不一样,经过半个多世纪的发展,编程语言已经基本形成了一套固定的类型,常见变量的数据类型有:整型、浮点型、布尔型等
Go语言中的每一个变量都有自己的类型,并且变量必须经过声明才能开始使用
3.1 Go语言中变量的声明声明格式
var 变量名 变量类型
变量声明以关键字var开头,变量类型放在变量的后面,行尾无需分号,举个例子
var name stringvar age intvar isOk bool3.2 批量声明每声明一个变量就需要写var关键字会比较繁琐,go语言中还支持批量变量声明:
var ( a string b int c bool d float32)3.3 变量的初始化Go语言在声明变量的时候,会自动对变量对应的内存区域进行初始化操作,每个变量会被初始化成其类型的默认值。例如整数和浮点型变量的默认值为0,字符串变量的默认值为空字符串,布尔型变量默认为false。切片、函数、指针变量的默认为nil
func main() {var (test01 stringtest02 inttest03 booltest04 float32)fmt.Println(test01, test02, test03, test04)} 0 false 0当然我们也可在声明变量的时候为其指定初始值。变量初始化的标准格式如下:
var 变量名 类型=表达式
例子
var name string="wql"var age int =18或者一次初始化多个变量
var name,age="wql",20有时我们会将变量的类型省略,这个时候编译器会根据等号右边的值来推导变量的类型完成初始化
var name = "wql"var age = 183.4 短变量声明在函数内部,可以使用更简略的:=方式声明并初始化变量
package main//告诉程序需要fmt包,fmt具有打印输出的函数import "fmt"var n intfunc main() {b := 123fmt.Println(b)}3.5匿名变量在使用多重赋值时,如果想要忽略某个值,可以使用匿名变量(anonymous variable)。匿名变量用一个下划线表示例如
package main//告诉程序需要fmt包,fmt具有打印输出的函数import "fmt"func main() {x, _ := foo()_, y := foo()fmt.Println("x=", x)fmt.Println("y=", y)}func foo() (int, string) {return 10, "wql"}123 123hello wqlx= 10y= wql匿名变量不占用命名空间,不会分配内存,所以匿名变量之间不存在重复声明
注意事项
函数外的每个语句必须以关键字开始(var、const、func等)
:=不能用在函数外
_多用于占位,表示忽略值
4.常量相对于变量,常量时恒定不变的值,多用于定义程序运行期间不会改变的哪些值。常量的声明和变量的声明非常类似,知识把var换成了const,常量在定义的时候必须赋值
const pi = 3.12
const e = 2.7
fmt.Println(pi, e)
1
2
3
在声明了pi和e这两个常量之后,在整个程序运行期间它们的值都不能再发生变化了
多个常量也可以一起声明
const( p1 = 2 p2 = 4 )const同时声明多个常量时,如果省略了值则表示和上面一行的值相同,例如
const( p3 = 200 p4 p5 )上面的实例中p3,p4,p5的值都是200
5.iotaiota是go语言的常量计数器,只能在常量的表达式中使用
iota在const关键字出现时将被重置为0,const中每新增一行常量声明将使iota计数一次,iota可以理解为const语句块中的行索引,使用iota能简化定义,在定义枚举时很有用
const ( n1 = iota //0 n2 //1 n3 //2 n4 //3 ) fmt.Println(n1, n2, n3, n4)0 1 2 3`几个常见的iota实例:
`使用下划线跳过某些值
const ( n1 = iota //0 n2 //1 _ n4 //3 )iota声明中间插队 const ( n1 = iota //0 n2 = 100 //100 n3 = iota //2 n4 //3 )也就是说有无初始值,iota都+1,常量有初始值就是初始值,没有初始值为iota+1
const ( _ = iota KB = 1 << (10 * iota) MB = 1 << (10 * iota) GB = 1 << (10 * iota) TB = 1 << (10 * iota) PB = 1 << (10 * iota) )多个iota定义在一行
const ( a, b = iota + 1, iota + 2 //1,2 c, d //2,3 e, f //3,4 )所以iota有两个特点
当遇见const出现被置为0
每新增一行就+1
总结到此这篇关于一篇文章学会GO语言中变量的文章就介绍到这了,更多相关GO语言变量内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- Free, easy-to-use, powerful lightweight note taking software evaluation: drafts, apple memo, flomo, keep, flowus, agenda, sidenote, workflow
- Ranking list of databases in July: mongodb and Oracle scores fell the most
- Redis 解决事务冲突之乐观锁和悲观锁
- [local differential privacy and random response code implementation] differential privacy code implementation series (13)
- 浮点数如何与0进行比较?
- ES6 modularization
- Gin integrated Alipay payment
- 深度学习7 Transformer系列实例分割Mask2Former
- 从0到1建设智能灰度数据体系:以vivo游戏中心为例
- go-zero微服务实战系列(九、极致优化秒杀性能)
猜你喜欢

IO flow: node flow and processing flow are summarized in detail.

Redis publish and subscribe

Comment configurer un accord

5G电视难成竞争优势,视频资源成中国广电最后武器

Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked

开源人张亮的 17 年成长路线,热爱才能坚持

【C语言】指针笔试题
![Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding](/img/4a/6763e3fbdeaf9de673fbe8eaf96858.png)
Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding

Xcode abnormal pictures cause IPA packet size problems

Opencv learning notes - linear filtering: box filtering, mean filtering, Gaussian filtering
随机推荐
selenium 浏览器(2)
TechSmith Camtasia studio 2022.0.2 screen recording software
Luo Gu - some interesting questions 2
Programmers exposed that they took private jobs: they took more than 30 orders in 10 months, with a net income of 400000
What are the concepts of union, intersection, difference and complement?
IO flow: node flow and processing flow are summarized in detail.
Helix Swarm中文包发布,Perforce进一步提升中国用户体验
十六进制
深度学习 网络正则化
LVGL 8.2 Menu
曝光一下阿里的工资待遇和职位级别
LVGL 8.2 Line wrap, recoloring and scrolling
Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
Five minutes of machine learning every day: how to use matrix to represent the sample data of multiple characteristic variables?
力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
毕业季-个人总结
Ffprobe common commands
Openresty redirection
Guitar Pro 8win10 latest guitar learning / score / creation
Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)