Golang 短变量声明操作符(:=)
Golang中的短变量声明操作符(:=)是用来创建具有适当名称和初始值的变量。使用这个操作符的主要目的是在函数中声明和初始化 局部变量 ,并缩小变量的范围。变量的类型由表达式的类型决定。var关键字也用于创建特定类型的变量。所以你可以说,在Golang中有两种创建变量的方法,如下。
- 使用var关键字
- 使用简短的变量声明操作符(:=)。
在这篇文章中,我们将只讨论短变量声明操作符。要了解var关键字,你可以参考 Go中的var关键字。 你也可以阅读 var关键字和短变量声明操作符之间的区别 ,以正确理解两者的使用。
使用短变量声明操作符的语法 。
variable_name := expression or value
在这里,你必须在声明后就初始化变量。但是使用var关键字,你可以避免在声明时进行初始化。 没有必要提及变量的类型。 右手边的表达式或值用于评估变量的类型。
例子: 在这里,我们使用short declaration操作符来声明变量,我们没有指定变量的类型。变量的类型是由 := 操作符右边的表达式的类型决定的。
// Go program to illustrate the use
// of := (short declaration
// operator)
package main
import "fmt"
func main() {
// declaring and initializing the variable
a := 30
// taking a string variable
Language: = "Go Programming"
fmt.Println("The Value of a is: ", a)
fmt.Println("The Value of Language is: ", Language)
}
输出
The Value of a is: 30
The Value of Language is: Go Programming
使用短声明操作符(:=)声明多个变量
短声明操作符也可以用来在单个声明中声明 相同类型或不同类型 的多个变量。这些变量的类型是由 :=
操作符右边的表达式来评估的。
例子
// Go program to illustrate how to use := short
// declaration operator to declare multiple
// variables into a single declaration statement
package main
import "fmt"
func main() {
// multiple variables of same type(int)
geek1, geek2, geek3 := 117, 7834, 5685
// multiple variables of different types
geek4, geek5, geek6 := "GFG", 859.24, 1234
// Display the value and
// type of the variables
fmt.Printf("The value of geek1 is : %d\n", geek1)
fmt.Printf("The type of geek1 is : %T\n", geek1)
fmt.Printf("\nThe value of geek2 is : %d\n", geek2)
fmt.Printf("The type of geek2 is : %T\n", geek2)
fmt.Printf("\nThe value of geek3 is : %d\n", geek3)
fmt.Printf("The type of geek3 is : %T\n", geek3)
fmt.Printf("\nThe value of geek4 is : %s\n", geek4)
fmt.Printf("The type of geek4 is : %T\n", geek4)
fmt.Printf("\nThe value of geek5 is : %f\n", geek5)
fmt.Printf("The type of geek5 is : %T\n", geek5)
fmt.Printf("\nThe value of geek6 is : %d\n", geek6)
fmt.Printf("The type of geek6 is : %T\n", geek6)
}
输出
The value of geek1 is : 117
The type of geek1 is : int
The value of geek2 is : 7834
The type of geek2 is : int
The value of geek3 is : 5685
The type of geek3 is : int
The value of geek4 is : GFG
The type of geek4 is : string
The value of geek5 is : 859.240000
The type of geek5 is : float64
The value of geek6 is : 1234
The type of geek6 is : int
重要提示 。
- 当
:=
操作符 左边的变量中至少有一个是新声明的,就可以使用短声明操作符。对于那些已经在同一个词组中声明的变量,短变量声明操作符的行为就像赋值。为了更好地理解这个概念,让我们举个例子。
例1: 下面的程序会出现错误,因为在:=
操作符的左边没有新的变量。
// Go program to illustrate the concept
// of short variable declaration
package main
import "fmt"
func main() {
// taking two variables
p, q := 100, 200
fmt.Println("Value of p ", p, "Value of q ", q)
// this will give an error as
// there are no new variable
// on the left-hand side of :=
p, q := 500, 600
fmt.Println("Value of p ", p, "Value of q ", q)
}
错误
./prog.go:17:10: no new variables on left side of :=
例2: 在下面的程序中,你可以看到这行代码 geek3, geek2 := 456, 200 可以正常工作,没有任何错误,因为在 := 操作符的左边至少有一个新变量,即 geek3 。
// Go program to show how to use
// short variable declaration operator
package main
import "fmt"
func main() {
// Here, short variable declaration acts
// as an assignment for geek1 variable
// because same variable present in the same block
// so the value of geek2 is changed from 100 to 200
geek1, geek2 := 78, 100
// here, := is used as an assignment for geek2
// as it is already declared. Also, this line
// will work fine as geek3 is newly created
// variable
geek3, geek2 := 456, 200
// If you try to run the commented lines,
// then compiler will gives error because
// these variables are already defined
// geek1, geek2 := 745, 956
// geek3 := 150
// Display the values of the variables
fmt.Printf("The value of geek1 and geek2 is : %d %d\n", geek1, geek2)
fmt.Printf("The value of geek3 and geek2 is : %d %d\n", geek3, geek2)
}
输出
The value of geek1 and geek2 is : 78 200
The value of geek3 and geek2 is : 456 200
- Go是一种强类型的语言,因为你不能把其他类型的值分配给已声明的变量。
例子
// Go program to show how to use
// short variable declaration operator
package main
import "fmt"
func main() {
// taking a variable of int type
z := 50
fmt.Printf("Value of z is %d", z)
// reassigning the value of string type
// it will give an error
z := "Golang"
}
错误 。
./prog.go:16:4: no new variables on left side of :=
./prog.go:16:7: cannot use “Golang” (type string) as type int in assignment
- 在一个简短的变量声明中,允许通过返回多个值的调用函数来初始化一组变量。或者你可以说变量也可以被赋值,在运行时被评估。
例子
// Here, math.Max function return
// the maximum number in i variable
i := math.Max(x, y)
本地变量还是全局变量?
在短变量声明操作符(:=)的帮助下, 你只能声明 只有块级范围的 局部变量 。一般来说,局部变量是在功能块内声明的。如果你试图用短声明操作符来声明全局变量,那么你会得到一个错误。
例1 :
// Go program to show the use of := operator
// to declare local variables
package main
import "fmt"
// using var keyword to declare
// and initialize the variable
// it is package or you can say
// global level scope
var geek1 = 900
// using short variable declaration
// it will give an error
geek2 := 200
func main() {
// accessing geek1 inside the function
fmt.Println(geek1)
// accessing geek2 inside the function
fmt.Println(geek2)
}
错误 。
./prog.go:15:1: syntax error: non-declaration statement outside function body
例2 :
// Go program to show the use of := operator
// to declare local variables
package main
import "fmt"
// using var keyword to declare
// and initialize the variable
// it is package or you can say
// global level scope
var geek1 = 900
func main() {
// using short variable declaration
// inside the main function
// it has local scope i.e. can't
// accessed outside the main function
geek2 := 200
// accessing geek1 inside the function
fmt.Println(geek1)
// accessing geek2 inside the function
fmt.Println(geek2)
}
输出
900
200