📜  Go 中的 var 关键字

📅  最后修改于: 2021-10-24 13:10:16             🧑  作者: Mango

Golang 中的var 关键字用于创建具有正确名称和初始值的特定类型的变量。在使用var关键字声明变量时,初始化是可选的,我们将在本文后面讨论。

句法:

var identifier type = expression

例子:

// here geek1 is the identifier 
// or variable name, int is the
// type and 200 is assigned value
var geek1 int = 200

如您所知,Go 是一种静态类型语言,但它仍然提供了一种工具,可以在声明变量时删除数据类型的声明,如下面的语法所示。这通常称为类型推断

句法:

var identifier = initialValue

例子:

var geek1 = 200

使用 var 关键字的多个变量声明

var 关键字还用于在一行中声明多个变量。您还可以为变量提供初始值,如下所示:

  • 使用 var 关键字和类型声明多个变量:
    var geek1, geek2, geek3, geek4 int
  • 使用 var 关键字以及类型和初始值声明多个变量:
    var geek1, geek2, geek3, geek4 int = 10, 20, 30, 40

笔记:

  • 您还可以使用类型推断(上面讨论过) ,让编译器知道类型,即在声明多个变量时可以选择删除类型。

    例子:

    var geek1, geek2, geek3, geek4 = 10, 20, 30.30, true
  • 您还可以使用多行使用 var 关键字声明和初始化不同类型的值,如下所示:

    例子:

    var(
         geek1 = 100
         geek2 = 200.57
         geek3 bool
         geek4 string = "GeeksforGeeks"
    )
    
  • 在声明期间使用类型时,您只能声明多个相同类型的变量。但是在声明期间删除类型您可以声明多个不同类型的变量。

    例子:

    // Go program to demonstrate the multiple 
    // variable declarations using var keyword
    package main
      
    import "fmt"
      
    func main() {
      
        // Multiple variables of the same type
        // are declared and initialized
        // in the single line along with type
        var geek1, geek2, geek3 int = 232, 784, 854
       
        // Multiple variables of different type
        // are declared and initialized
        // in the single line without specifying
        // any type
        var geek4, geek5, geek6 = 100, "GFG", 7896.46
      
           
       // Display the values of the variables
       fmt.Printf("The value of geek1 is : %d\n", geek1)
                                             
       fmt.Printf("The value of geek2 is : %d\n", geek2)
                                             
       fmt.Printf("The value of geek3 is : %d\n", geek3)
      
       fmt.Printf("The value of geek4 is : %d\n", geek4)
                                             
       fmt.Printf("The value of geek5 is : %s\n", geek5)
                                             
       fmt.Printf("The value of geek6 is : %f", geek6)
                                                
    }
    

    输出:

    The value of geek1 is : 232
    The value of geek2 is : 784
    The value of geek3 is : 854
    The value of geek4 is : 100
    The value of geek5 is : GFG
    The value of geek6 is : 7896.460000
    

关于 var 关键字的要点:

  • 在使用 var 关键字声明变量期间,您可以删除 type 或 = 表达式,但不能同时删除两者。如果你这样做,那么编译器会给出一个错误。
  • 如果您删除了表达式,则默认情况下,变量将包含数字的零值和布尔值的false字符串“”和 nil 的接口和引用类型。所以,Go 语言中没有未初始化变量的概念。

    例子:

    // Go program to illustrate
    // concept of var keyword
    package main
        
    import "fmt"
        
    func main() {
       
        // Variable declared but
        // no initialization
        var geek1 int
        var geek2 string
        var geek3 float64
        var geek4 bool
       
        // Display the zero-value of the variables
        fmt.Printf("The value of geek1 is : %d\n", geek1)
                                   
        fmt.Printf("The value of geek2 is : %s\n", geek2)
       
        fmt.Printf("The value of geek3 is : %f\n", geek3)
      
        fmt.Printf("The value of geek4 is : %t", geek4)
                                      
    }
    

    输出:

    The value of geek1 is : 0
    The value of geek2 is : 
    The value of geek3 is : 0.000000
    The value of geek4 is : false