📜  去变量

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

一个典型的程序使用在其执行过程中可能会改变的各种值。例如,一个对用户输入的值执行一些操作的程序。一个用户输入的值可能与另一用户输入的值不同。因此,这使得必须使用变量,因为另一个用户可能不会使用相同的值。当用户输入一个将在操作过程中使用的新值时,可以临时存储在计算机的随机存取存储器中,这部分内存中的这些值在整个执行过程中会发生变化,因此出现了另一个已知的术语作为变量。所以基本上,变量是可以在运行时更改的信息的占位符。变量允许检索和操作存储的信息。

命名变量的规则:

  • 变量名必须以字母或下划线 (_) 开头。并且名称可能包含字母“az”或“AZ”或数字 0-9 以及字符“_”。
    Geeks, geeks, _geeks23  // valid variable
    123Geeks, 23geeks      // invalid variable
    
  • 变量名不应以数字开头。
    234geeks  // illegal variable 
  • 变量名区分大小写。
    geeks and Geeks are two different variables
  • 关键字不允许用作变量名。
  • 变量名称的长度没有限制,但建议仅使用 4-15 个字母的最佳长度。

声明一个变量

在 Go 语言中,变量以两种不同的方式创建:

  1. 使用 var 关键字:在 Go 语言中,变量是使用特定类型的var关键字创建的,与名称连接并提供其初始值。

    句法:

    var variable_name type = expression

    要点:

    • 在上面的语法中, type=表达式中的任何一个都可以删除,但不能同时删除变量声明中的两个。
    • 如果删除了类型,则变量的类型由表达式中的值初始化确定。

      例子:

      // Go program to illustrate 
      // concept of variable
      package main
        
      import "fmt"
        
      func main() {
        
      // Variable declared and 
      // initialized without the 
      // explicit type
      var myvariable1 = 20
      var myvariable2 = "GeeksforGeeks"
      var myvariable3 = 34.80
        
      // Display the value and the
      // type of the variables
      fmt.Printf("The value of myvariable1 is : %d\n",
                                        myvariable1)
                                            
      fmt.Printf("The type of myvariable1 is : %T\n",
                                        myvariable1)
            
      fmt.Printf("The value of myvariable2 is : %s\n",
                                            myvariable2)
                                              
      fmt.Printf("The type of myvariable2 is : %T\n",
                                        myvariable2)
            
      fmt.Printf("The value of myvariable3 is : %f\n",
                                            myvariable3)
                                              
      fmt.Printf("The type of myvariable3 is : %T\n",
                                        myvariable3)
            
      }
      

      输出:

      The value of myvariable1 is : 20
      The type of myvariable1 is : int
      The value of myvariable2 is : GeeksforGeeks
      The type of myvariable2 is : string
      The value of myvariable3 is : 34.800000
      The type of myvariable3 is : float64
      
    • 如果表达式被删除,则变量为类型保留零值,例如数字为零,布尔值为 false,字符串为“” ,接口和引用类型为 nil。所以, Go 语言中没有未初始化变量的概念。

      例子:

      // Go program to illustrate
      // concept of variable
      package main
         
      import "fmt"
         
      func main() {
        
          // Variable declared and 
          // initialized without expression
          var myvariable1 int
          var myvariable2 string
          var myvariable3 float64
        
          // Display the zero-value of the variables
          fmt.Printf("The value of myvariable1 is : %d\n",
                                           myvariable1)
        
          fmt.Printf("The value of myvariable2 is : %s\n",
                                           myvariable2)
        
          fmt.Printf("The value of myvariable3 is : %f",
                                           myvariable3)
      }
      

      输出:

      The value of myvariable1 is : 0
      The value of myvariable2 is : 
      The value of myvariable3 is : 0.000000
      
    • 如果使用类型,则允许在单个声明中声明多个相同类型的变量。

      例子:

      // Go program to illustrate
      // concept of variable
      package main
      import "fmt"
         
      func main() {
         
          // Multiple variables of the same type
          // are declared and initialized
          // in the single line
          var myvariable1, myvariable2, myvariable3 int = 2, 454, 67
         
         // Display the values of the variables
         fmt.Printf("The value of myvariable1 is : %d\n",
                                             myvariable1)
        
         fmt.Printf("The value of myvariable2 is : %d\n",
                                             myvariable2)
        
         fmt.Printf("The value of myvariable3 is : %d",
                                            myvariable3)
      }
      

      输出:

      The value of myvariable1 is : 2
      The value of myvariable2 is : 454
      The value of myvariable3 is : 67
      
    • 如果删除类型,则允许在单个声明中声明多个不同类型的变量。变量的类型由初始化值决定。

      例子:

      // Go program to illustrate
      // concept of variable
      package main
      import "fmt"
        
      func main() {
        
      // Multiple variables of different types
      // are declared and initialized in the single line
      var myvariable1, myvariable2, myvariable3 = 2, "GFG", 67.56
        
      // Display the value and 
      // type of the variables
      fmt.Printf("The value of myvariable1 is : %d\n",
                                          myvariable1)
        
      fmt.Printf("The type of myvariable1 is : %T\n",
                                         myvariable1)
        
      fmt.Printf("\nThe value of myvariable2 is : %s\n",
                                           myvariable2)
        
      fmt.Printf("The type of myvariable2 is : %T\n",
                                         myvariable2)
        
      fmt.Printf("\nThe value of myvariable3 is : %f\n",
                                            myvariable3)
        
      fmt.Printf("The type of myvariable3 is : %T\n",
                                         myvariable3)
      }
      

      输出:

      The value of myvariable1 is : 2
      The type of myvariable1 is : int
      
      The value of myvariable2 is : GFG
      The type of myvariable2 is : string
      
      The value of myvariable3 is : 67.560000
      The type of myvariable3 is : float64
      
    • 您可以通过返回多个值的调用函数来初始化一组变量。

      例子:

      // Here, os.Open function return a
      // file in i variable and an error
      // in j variable
      var i, j = os.Open(name)
      
  2. 使用短变量声明:在函数中声明和初始化的局部变量使用短变量声明来声明。

    句法:

    variable_name:= expression

    注意:请不要在:==之间混淆,因为:=是声明而=是赋值。

    要点:

    • 在上面的表达式中,变量的类型是由表达式的类型决定的。

      例子:

      // Go program to illustrate
      // concept of variable
      package main
      import "fmt"
        
      func main() {
        
      // Using short variable declaration
      myvar1 := 39 
      myvar2 := "GeeksforGeeks" 
      myvar3 := 34.67
        
      // Display the value and type of the variables
      fmt.Printf("The value of myvar1 is : %d\n", myvar1)
      fmt.Printf("The type of myvar1 is : %T\n", myvar1)
        
      fmt.Printf("\nThe value of myvar2 is : %s\n", myvar2)
      fmt.Printf("The type of myvar2 is : %T\n", myvar2)
        
      fmt.Printf("\nThe value of myvar3 is : %f\n", myvar3)
      fmt.Printf("The type of myvar3 is : %T\n", myvar3)
      }
      

      输出:

      The value of myvar1 is : 39
      The type of myvar1 is : int
      
      The value of myvar2 is : GeeksforGeeks
      The type of myvar2 is : string
      
      The value of myvar3 is : 34.670000
      The type of myvar3 is : float64
      
    • 由于其简洁性和灵活性,大多数局部变量通过使用短变量声明来声明和初始化。
    • 变量的 var 声明用于那些需要与初始化表达式不同的显式类型的局部变量,或者用于那些稍后分配值且初始化值不重要的变量。
    • 使用短变量声明,您可以在单个声明中声明多个变量。

      例子:

      // Go program to illustrate
      // concept of variable
      package main
      import "fmt"
        
      func main() {
        
      // Using short variable declaration
      // Multiple variables of same types
      // are declared and initialized in 
      // the single line
      myvar1, myvar2, myvar3 := 800, 34, 56
        
      // Display the value and 
      // type of the variables
      fmt.Printf("The value of myvar1 is : %d\n", myvar1)
      fmt.Printf("The type of myvar1 is : %T\n", myvar1)
        
      fmt.Printf("\nThe value of myvar2 is : %d\n", myvar2)
      fmt.Printf("The type of myvar2 is : %T\n", myvar2)
        
      fmt.Printf("\nThe value of myvar3 is : %d\n", myvar3)
      fmt.Printf("The type of myvar3 is : %T\n", myvar3)
      }
      

      输出:

      The value of myvar1 is : 800
      The type of myvar1 is : int
      
      The value of myvar2 is : 34
      The type of myvar2 is : int
      
      The value of myvar3 is : 56
      The type of myvar3 is : int
      
    • 在简短的变量声明中,您可以通过返回多个值的调用函数来初始化一组变量。

      例子:

      // Here, os.Open function return 
      // a file in i variable and an 
      // error in j variable
      i, j := os.Open(name)
      
    • 仅当对于已在同一词法块中声明的那些变量时,短变量声明才像赋值。在外部块中声明的变量将被忽略。并且至少有一个变量是这两个变量中的一个新变量,如下例所示。

      例子:

      // Go program to illustrate
      // concept of variable
      package main
      import "fmt"
        
      func main() {
        
      // Using short variable declaration
      // Here, short variable declaration acts
      // as an assignment for myvar2 variable
      // because same variable present in the same block
      // so the value of myvar2 is changed from 45 to 100
      myvar1, myvar2 := 39, 45 
      myvar3, myvar2 := 45, 100
        
      // If you try to run the commented lines,
      // then compiler will gives error because
      // these variables are already defined
      // myvar1, myvar2 := 43, 47
      // myvar2:= 200
        
      // Display the values of the variables
      fmt.Printf("The value of myvar1 and myvar2 is : %d %d\n",
                                                myvar1, myvar2)
                                                  
      fmt.Printf("The value of myvar3 and myvar2 is : %d %d\n",
                                                myvar3, myvar2)
      }
      

      输出:

      The value of myvar1 and myvar2 is : 39 100
      The value of myvar3 and myvar2 is : 45 100
      
    • 使用短变量声明,您可以在单个声明中声明多个不同类型的变量。这些变量的类型由表达式决定。

      例子:

      // Go program to illustrate
      // concept of variable
      package main
      import "fmt"
        
      func main() {
        
      // Using short variable declaration
      // Multiple variables of different types
      // are declared and initialized in the single line
      myvar1, myvar2, myvar3 := 800, "Geeks", 47.56
        
      // Display the value and type of the variables
      fmt.Printf("The value of myvar1 is : %d\n", myvar1)
      fmt.Printf("The type of myvar1 is : %T\n", myvar1)
        
      fmt.Printf("\nThe value of myvar2 is : %s\n", myvar2)
      fmt.Printf("The type of myvar2 is : %T\n", myvar2)
        
      fmt.Printf("\nThe value of myvar3 is : %f\n", myvar3)
      fmt.Printf("The type of myvar3 is : %T\n", myvar3)
        
      }
      

      输出:

      The value of myvar1 is : 800
      The type of myvar1 is : int
      
      The value of myvar2 is : Geeks
      The type of myvar2 is : string
      
      The value of myvar3 is : 47.560000
      The type of myvar3 is : float64