Go Variables

A typical program uses various values that may change during its execution. For Example, a program that performs some operations on the values entered by the user. The values entered by one user may differ from those entered by another user. Hence this makes it necessary to use variables as another user may not use the same values. When a user enters a new value that will be used in the process of operation, can store temporarily in the Random Access Memory of the computer and these values in this part of memory vary throughout the execution and hence another term for this came which is known as Variables. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allow to Retrieve and Manipulate the stored information.

Rules for Naming Variables:

  • Variable names must begin with a letter or an underscore(_). And the names may contain the letters ‘a-z’ or ’A-Z’ or digits 0-9 as well as the character ‘_’.
    Geeks, geeks, _geeks23  // valid variable
    123Geeks, 23geeks      // invalid variable
    
  • A variable name should not start with a digit.
    234geeks  // illegal variable 
  • The name of the variable is case sensitive.
    geeks and Geeks are two different variables
  • Keywords is not allowed to use as a variable name.
  • There is no limit on the length of the name of the variable, but it is advisable to use an optimum length of 4 – 15 letters only.

Declaring a Variable

In Go language variables are created in two different ways:



  1. Using var keyword: In Go language, variables are created using var keyowrd of a particular type, connected with name and provide its initial value.

    Syntax:

    var variable_name type = expression

    Important Points:

    • In the above syntax, either type or = expression can remove, but not both, can remove in the declaration of a variable.
    • If the type is removed, then the type of the variable is determined by the value-initialize in the expression.

      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

      Output:

      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
      
    • If the expression is removed, then the variable holds zero-value for the type like zero for the number, false for Booleans, “” for strings, and nil for interface and reference type. So, there is no such concept of an uninitialized variable in Go language.

      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

      Output:

      The value of myvariable1 is : 0
      The value of myvariable2 is : 
      The value of myvariable3 is : 0.000000
      
    • If you use type, then you are allowed declare multiple variable of the same type in the single declaration.

      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

      Output:

      The value of myvariable1 is : 2
      The value of myvariable2 is : 454
      The value of myvariable3 is : 67
      
    • If you remove type, then you are allowed to declare multiple variables of a different type in the single declaration. The type of variables is determined by the initialized values.

      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

      Output:


      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
      
    • You are allowed to initialize a set of variables by the calling function that returns multiple values.

      Example:

      // Here, os.Open function return a
      // file in i variable and an error
      // in j variable
      var i, j = os.Open(name)
      
  2. Using short variable declaration: The local variables which are declared and initialize in the functions are declared by using short variable declaration.

    Syntax:

    variable_name:= expression

    Note: Please don’t confuse in between := and = as := is a declaration and = is assignment.

    Important Points:

    • In the above expression, the type of the variable is determined by the type of the expression.

      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

      Output:

      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
      
    • Most of the local variables are declared and initialized by using short variable declarations due to their brevity and flexibility.
    • The var declaration of variables are used for those local variables which need an explicit type that differs from the initializer expression, or for those variables whose values are assigned later and the initialized value is unimportant.
    • Using short variable declaration you are allowed to declare multiple variables in the single declaration.

      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

      Output:

      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
      
    • In a short variable declaration, you are allowed to initialize a set of variables by the calling function that returns multiple values.

      Example:

      // Here, os.Open function return 
      // a file in i variable and an 
      // error in j variable
      i, j := os.Open(name)
      
    • A short variable declaration acts like an assignment only when for those variables that are already declared in the same lexical block. The variables that are declared in the outer block are ignored. And at least one variable is a new variable out of these two variables as shown in the below example.

      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

      Output:

      The value of myvar1 and myvar2 is : 39 100
      The value of myvar3 and myvar2 is : 45 100
      
    • Using short variable declaration you are allowed to declare multiple variables of different types in the single declaration. The type of these variables are determined by the expression.

      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

      Output:

      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
      


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.