golang 变量_Golang中的变量

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 main
import "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 main

import "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 main

import "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 main

import "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 main

import "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 main

import "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: 0
height: 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: 20
name: includehelp

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

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

翻译自: https://www.includehelp.com/golang/variables-in-golang.aspx

golang 变量

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值