📜  Go 中的短变量声明运算符 (:=)

📅  最后修改于: 2021-10-25 02:40:37             🧑  作者: Mango

Golang 中的短变量声明运算符(:=) 用于创建具有正确名称和初始值的变量。使用此运算符的主要目的是在函数内部声明和初始化局部变量,并缩小变量的范围。变量的类型由表达式的类型决定。 var 关键字也用于创建特定类型的变量。所以可以说在Golang中有两种创建变量的方法如下:

  • 使用 var 关键字
  • 使用短变量声明运算符(:=)

在本文中,我们将只讨论短变量声明运算符。要了解 var 关键字,您可以参考Go 中的 var 关键字。您还可以阅读var 关键字和短变量声明运算符之间区别,以正确使用两者。

使用短变量声明运算符的语法:

variable_name := expression or value

在这里,您必须在声明后立即初始化变量。但是使用 var 关键字可以避免在声明时进行初始化。无需提及变量的类型。右侧的表达式或值用于评估变量的类型。

示例:在这里,我们使用短声明运算符来声明变量,并且没有指定变量的类型。变量的类型由:= 运算符右侧的表达式类型决定。

Go
// 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)
   
}


Go
// 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)
  
}


Go
// 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)
}


Go
// 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)
}


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"
}


Go
// 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)
       
}


Go
// 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)
       
}


输出:

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)
}

错误:

示例 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"
}

错误:

  • 在简短的变量声明中,允许通过返回多个值的调用函数来初始化一组变量。或者您可以说变量也可以被分配在运行时评估的值。
    例子:
// 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)
       
}

错误:

示例 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