当前位置:网站首页>Go变量的声明与赋值
Go变量的声明与赋值
2022-06-26 13:57:00 【m0_52339560】
Go变量的声明与赋值
声明变量的三种形式
var 变量名 变量类型
示例:
var name int = 0 //声明了一个int型的变量,其标识符为name println(int)
var 变量名:通过声明变量类型来声明变量,编译器会通过初始化的值来自行推断变量类型。
示例:省略了变量类型,那么必须在声明时给定初值,能够让编译器判断其类型,否则语法错
missing variable type or initializationsyntax。var name = 0 var name1 = "Hello go" println(name, name1) //输出为0 Hello go print(fmt.Sprintf("%T---%T", name, name1))//打印两个变量的类型 //int---string name为int型,name1为string型
- 使用
:=声明变量并赋初值
示例:
name := "Hello go" println(name)上述代码等价于:
var name string name = "Hello go" println(name)注意:使用
:=相当于声明变量和为变量初始化同时进行,所以对于表达式name:="Hello go",如果变量name此前已经声明过,则不允许再次声明。如果声明,则语法检测器会显示错误no new variables on left side of :=。
多变量声明
如果同时要声明多个相同类型的变量,可以采用如下型式:
var name1, name2, name3 int = 1, 2, 3
如果要同时声明多个相同或不同类型的变量,则可以采用如下形式:
var name1, name2, name3 = 123, 2.0, "Hello go"
赋值时,等号右侧的多个数值或字符串根据它们的相对位置,依次赋值给等号左侧的变量
全局变量的声明
声明全局变量依然可以使用之前介绍的三种变量声明的方法。这里介绍全局变量声明的另外一种写法:
var(
name1 int
name2 string
name3 float32
)
局部变量的注意事项
对于局部变量,其声明在函数体内,一旦被声明,那么它必须在后面的代码段中被使用,否则会出现编译错。
而对于全局变量,它可以被声明而不被使用。
标识符_
对于表示符_,常用来抛弃不需要的值。比如函数func返回两个值a, b,不需要b的值,那么可以这样写:
a, _ = func()
与python的_类似。但是Go中的_不能够被读取,只能向该变量中写入值。而在python中,可以读取_中的值。
参考资料
- https://www.runoob.com/go/go-variables.html
边栏推荐
- Oracle ASMM and AMM
- Obtain information about hard disk and volume or partition (capacity, ID, volume label name, etc.)
- A solution to the problem that the display of newff function in neural network cannot be converted from double to struct
- MySQL主从复制与读写分离
- Intellij IDEA--格式化SQL文件的方法
- Win10 home vs pro vs enterprise vs enterprise LTSC
- NAACL2022:(代码实践)好的视觉引导促进更好的特征提取,多模态命名实体识别(附源代码下载)...
- 赠书 | 《认知控制》:我们的大脑如何完成任务?
- 从Celsius到三箭:加密百亿巨头们的多米诺,史诗级流动性的枯竭
- 大学生值得珍藏的实用网站推荐
猜你喜欢

备战数学建模31-数据插值与曲线拟合3

MySQL master-slave replication and read-write separation

PostGIS create spatial database

服务器创建虚拟环境跑代码

How to mount cloud disks in ECS

Matlab programming related knowledge

Naacl2022: (code practice) good visual guidance promotes better feature extraction, multimodal named entity recognition (with source code download)

Sword finger offer 06.24.35 Linked list

Eigen(3):error: ‘Eigen’ has not been declared

Chinese output of PostGIS console is garbled
随机推荐
Equation derivation: second order active bandpass filter design! (download: Tutorial + schematic + Video + code)
Combat readiness mathematical modeling 31 data interpolation and curve fitting 3
数学建模经验分享:国赛美赛对比/选题参考/常用技巧
Related knowledge of libsvm support vector machine
ArcGIS secondary development - arcpy delete layer
启动Redis报错:Could not create Server TCP listening socket *:6379: bind: Address already in use–解决办法
Sharing ideas for a quick switch to an underlying implementation
Setup instance of layout manager login interface
Oracle11g database import and export method tutorial [easy to understand]
Sword finger offer 40.41 Sort (medium)
Usage of unique function
BP neural network for prediction
Knowledge about adsorption
SwiftUI找回丢失的列表视图(List)动画
Correlation of XOR / and
A remove the underline from the label
服务器创建虚拟环境跑代码
Practical website recommendations worth collecting for College Students
Summary of decimal point of amount and price at work and pit
Deploy the flask environment using the pagoda panel