当前位置:网站首页>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
边栏推荐
- Flex & Bison 开始
- (improved) bubble sorting and (improved) cocktail sorting
- idea快捷键
- Leaflet loading ArcGIS for server map layers
- Excerpt from three body
- Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法
- Is it safe to open an online stock account? Somebody give me an answer
- One article of the quantification framework backtrader read observer
- Can wptx64 be uninstalled_ Which software of win10 can be uninstalled
- How to mount cloud disks in ECS
猜你喜欢

Leaflet loading ArcGIS for server map layers

Related knowledge of libsvm support vector machine

GDAL multiband synthesis tool

Where do people get their top energy?

The JVM outputs GC logs, causing the JVM to get stuck. I am stupid

'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册

15 bs对象.节点名称.节点名称.string 获取嵌套节点内容

Caelus - full scene offline mixed Department solution

Chinese output of PostGIS console is garbled

Eigen(3):error: ‘Eigen’ has not been declared
随机推荐
Eigen(3):error: ‘Eigen’ has not been declared
datasets Dataset类(2)
IP certificate application process of sectigo
方程推导:二阶有源带通滤波器设计!(下载:教程+原理图+视频+代码)
Flex & Bison 开始
Oracle ASMM和AMM
Combat readiness mathematical modeling 32 correlation analysis 2
Online bull Blogger
ArcGIS secondary development method - layer related operations (add, modify)
国信证券的排名如何?办理股票开户安全吗?
一篇抄十篇,CVPR Oral被指大量抄袭,大会最后一天曝光!
详解C语言编程题:任意三条边能否构成三角形,输出该三角形面积并判断其类型
Extended hooks
PostGIS create spatial database
Electron
《三体》摘录
信息学奥赛一本通 1405:质数的和与积 (思维题)
SwiftUI找回丢失的列表视图(List)动画
Two dimensional DFS
C语言刷题随记 —— 乒乓球比赛