Golang var关键字

Golang var关键字

在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"
)
  • 在声明过程中使用类型时,你只允许声明同一类型的多个变量。但是在声明过程中删除type,你可以声明不同类型的多个变量。

示例:

// 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关键字声明变量的过程中,你可以删除类型或=表达式,但不能同时删除。如果你这样做,那么编译器将给出一个错误。
  • 如果你删除了表达式,那么该变量将默认包含数字的零值和布尔运算的假值,字符串的””和接口及引用类型的””。所以,在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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程