当前位置:网站首页>Declaration and assignment of go variables
Declaration and assignment of go variables
2022-06-26 14:47:00 【m0_ fifty-two million three hundred and thirty-nine thousand fi】
Go Declaration and assignment of variables
List of articles
Three forms of declaring variables
var Variable name Variable type
Example :
var name int = 0 // Declared a int Type variables , Its identifier is name println(int)
var Variable name: Declare variables by declaring variable types , The compiler will infer the variable type by initializing the value .
Example : The variable type is omitted , Then the initial value must be given at the time of declaration , Can let the compiler judge its type , Otherwise the syntax is wrong
missing variable type or initializationsyntax.var name = 0 var name1 = "Hello go" println(name, name1) // Output is 0 Hello go print(fmt.Sprintf("%T---%T", name, name1))// Print the types of the two variables //int---string name by int type ,name1 by string type
- Use
:=Declare variables and assign initial values
Example :
name := "Hello go" println(name)The above code is equivalent to :
var name string name = "Hello go" println(name)Be careful : Use
:=It is equivalent to declaring variables and initializing variables at the same time , So for expressionsname:="Hello go", If the variablenameIt has been stated before , It is not allowed to declare again . If you declare , The syntax detector will display an errorno new variables on left side of :=.
Multivariable declaration
If you want to declare multiple variables of the same type at the same time , The following types can be used :
var name1, name2, name3 int = 1, 2, 3
If you want to declare multiple variables of the same or different types at the same time , It can take the form of :
var name1, name2, name3 = 123, 2.0, "Hello go"
assignment , Multiple numbers or strings to the right of the equal sign depend on their relative positions , Assign values to the variables to the left of the equal sign
Declaration of global variables
Global variables can still be declared using the three variable declaration methods described above . Here is another way to write a global variable declaration :
var(
name1 int
name2 string
name3 float32
)
Considerations for local variables
For local variables , Its declaration is in the function body , Once declared , Then it must be used in the following code segment , Otherwise, compilation errors will occur .
And for global variables , It can be declared without being used .
identifier _
For designators _, Often used to discard unwanted values . Like functions func Returns two values a, b, Unwanted b Value , Then you can write like this :
a, _ = func()
And python Of _ similar . however Go Medium _ Cannot be read , Only values can be written to this variable . And in the python in , Can read _ The value in .
Reference material
- https://www.runoob.com/go/go-variables.html
边栏推荐
- Combat readiness mathematical modeling 32 correlation analysis 2
- Eigen(3):error: ‘Eigen’ has not been declared
- 国信证券的排名如何?办理股票开户安全吗?
- 在云服务器中云磁盘如何挂载
- A标签去掉下划线
- Sectigo的IP证书申请流程
- Practice with the topic of bit operation force deduction
- Installation tutorial about origin2019
- Two dimensional DFS
- Sword finger offer 05.58 Ⅱ string
猜你喜欢

Electron

From Celsius to the three arrows: encrypting the domino of the ten billion giants, and drying up the epic liquidity

这才是优美的文件系统挂载方式,亲测有效

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

MySQL master-slave replication and read-write separation

赠书 | 《认知控制》:我们的大脑如何完成任务?

Where do people get their top energy?

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

Sword finger offer 40.41 Sort (medium)

The engine "node" is inconsistent with this module
随机推荐
Two point answer, 01 score planning (mean / median conversion), DP
K gold Chef (two conditions, two points and difference)
Numpy基本使用
ArcGIS batch export layer script
Atcoder 238
Electron
A remove the underline from the label
大学生值得珍藏的实用网站推荐
券商经理给的开户链接办理股票开户安全吗?我想开个户
Datasets dataset class (2)
Eigen(3):error: ‘Eigen’ has not been declared
GDAL multiband synthesis tool
Pycharm远程连接服务器来跑代码
获取两个dataframe的交并差集
房东拿租金去还房贷是天经地义的嘛
数学建模经验分享:国赛美赛对比/选题参考/常用技巧
Where do people get their top energy?
MySQL master-slave replication and read-write separation
wptx64能卸载吗_win10自带的软件哪些可以卸载
工作上对金额价格类小数点的总结以及坑