博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
golang 变量_Golang中的变量
阅读量:2526 次
发布时间:2019-05-11

本文共 5090 字,大约阅读时间需要 16 分钟。

golang 变量

Golang中的变量 (Variable in Golang)

Variables in go are used to give the name to a memory location for storing the data of specific type.

go中的变量用于将名称指定给用于存储特定类型数据的存储位置。

变量声明 (Variable Declarations)

There are various ways to declare the variables in Golang.

有多种方法可以在Golang中声明变量。

1) Declare single variable at a time

1)一次声明一个变量

Syntax:

句法:

var variable_name type

Example:

例:

package mainimport "fmt"func main() {      var student_weight int // variable declaration    fmt.Println("Student weight is: ", student_weight)}

In the above example, the statement var student_weight int declares a variable named student_weight of type int.

在上面的示例中,语句var student_weight int声明了一个类型为int的名为student_weight的变量。

Output

输出量

Student weight is:  0

Here, Output is zero (0). Reason being, when a variable is not assigned with any value. go compiler automatically initializes default value of variable type. Here, the variable type is int so the default value of int is zero (0).

此处,输出为零(0)。 原因是,当变量未分配任何值时。 go编译器会自动初始化变量类型的默认值。 此处,变量类型为int,因此int的默认值为零(0)。

2) Declare a variable with initial value

2)声明一个带有初始值的变量

Syntax:

句法:

var variable_name type = initial_value

Example:

例:

package mainimport "fmt"func main() {  	// variable declaration with 20 initial value	var student_weight int = 20	fmt.Println("Student weight is: ", student_weight)}

In the above example, The statement var student_weight int = 20 declares a variable named student_weight of type int with initial value 20.

在上面的示例中,语句var student_weight int = 20声明了一个类型为int且初始值为20的名为student_weight的变量。

Output

输出量

Student weight is: 20

3) Multiple variable declarations

3)多个变量声明

In go, we can declare multiple variables of the same type in a single statement.

可以在一个语句中声明相同类型的多个变量。

Syntax:

句法:

var variable_name_1, variable_name_2, variable_name_3 type

Example:

例:

package mainimport "fmt"func main() {  	// three variable declaration	var age, height, length int  	fmt.Printf("age: %d, height: %d, length: %d", age, height, length)}

In the above example, the statement var age, height, length int declares three variables named age, height, and length of type int.

在上面的示例中,语句var age,height,length int声明了三个变量,分别为age , height和int类型的length 。

Output

输出量

age: 0, height: 0, length: 0

Here, Go compiler assigns zero value (default value of int) to these variables.

在这里,Go编译器为这些变量分配了零值( int的默认值)。

4) Multiple variable declarations with initial value

4)具有初始值的多个变量声明

In go, we can declare multiple variables of the same type with an initial value in a single statement.

在执行过程中,我们可以在一个语句中声明一个具有初始值的相同类型的多个变量。

Syntax:

句法:

var variable_name_1, variable_name_2,      variable_name_3 type = value_1, value_2, value_3

Example:

例:

package mainimport "fmt"func main() {  	// three variable declaration with initial value	var age, height, length int  = 10,  20, 30 	fmt.Printf("age: %d, height: %d, length: %d", age, height, length)}

In the above example, the statement var age, height, length int declares three variables named age, height, and length of type int with initial values 10, 20, 30 respectively.

在上面的示例中,语句var age,height,length int声明了三个变量,分别名为age , height和int类型的length ,其初始值分别为10、20、30

Output

输出量

age: 10, height: 20, length: 30

5) Type inference

5)类型推断

In go, you can declare a variable with initial value without mentioning type. Go will automatically be able to infer the type of that variable using initial value.

使用go时,您可以声明具有初始值的变量而无需提及类型。 Go会自动使用初始值来推断该变量的类型。

Syntax:

句法:

var variable_name = initial_value

Example:

例:

package mainimport "fmt"func main() {  	// variable declaration with 20 initial value	var student_weight = 20	fmt.Println("Student weight is: ", student_weight)}

Output

输出量

Student weight is: 20

In the above example, go can infer it of type int.

在上面的示例中,go可以推断出int类型。

6) Declare a variable and assign value later

6)声明变量并稍后分配值

In go, you can declare variables and assign values to variables later.

在运行中,您可以声明变量并在以后为变量分配值。

Example:

例:

package mainimport "fmt"func main() {      var height, weight int    fmt.Printf("height: %d, weight: %d\n",  height, weight)    height = 8    weight = 50    fmt.Printf("height: %d, weight: %d",  height, weight)}

Output

输出量

height: 0, weight: 0height: 8, weight: 50

In the above example, 1st we declare the height and weight variables. Here, go compiler assigns a default value of int. that's why 1st output has 0 value of height and weight.

在上面的例子中,1 我们声明的高度和重量的变量。 在这里,go编译器将分配默认值int 。 这就是为什么第一个输出的height和weight值为0的原因。

Later we assign value to these variables as 8 to height and 50 to weight. So the second output is " height: 8, weight: 50".

之后,我们将这些变量的值赋给高度 8和权重 50。 所以第二个输出是“身高:8,体重:50”

7) Short hand declaration

7)空手申报

In short hand declaration, we use := operator.

简而言之,我们使用:=运算符。

Syntax:

句法:

variable_name := initial_value

Example:

例:

package main      import "fmt"func main() {      height, weight := 10, 20    fmt.Printf("height: %d, weight: %d\n",  height, weight)    name := "includehelp"    fmt.Printf("name: %s", name)}

Output

输出量

height: 10, weight: 20name: includehelp

Note: Short hand declaration requires initial value of all the variables.

注意:简写声明需要所有变量的初始值。

翻译自:

golang 变量

转载地址:http://bfvzd.baihongyu.com/

你可能感兴趣的文章
基于Extjs+SpringMVC+MyBatis+Oracle的B/S信息系统简化开发思路
查看>>
【python】字典的嵌套
查看>>
微信运营:必须收藏的101条万能微信标题公式
查看>>
【XLL 框架库函数】 TempMissing/TempMissing12
查看>>
利用VS自带发布功能实现web项目快速部署
查看>>
第七讲 数组动手动脑和课后作业
查看>>
zencart 对首页静态化处理
查看>>
50多条mysql数据库优化建议
查看>>
crontab 详细用法 定时任务
查看>>
配置流行为
查看>>
js数组的迭代
查看>>
Maven系列--"maven-compiler-plugin"的使用、Maven之Surefire插件
查看>>
20165202 实验一 Java开发环境的熟悉
查看>>
Linux篇---Grep和正则匹配
查看>>
搭建SSM项目时报错(org.springframework.jdbc.CannotGetJdbcConnectionException)
查看>>
关于RPC
查看>>
获得select下拉框的值
查看>>
[H5-Compress-Image]利用canvas实现 javascript 图片压缩处理_基于requirejs模块化的代码实现...
查看>>
漏洞利用之Metasploit使用过程
查看>>
我在 B 站学习深度学习(生动形象,跃然纸上)
查看>>