Difference between var keyword and short declaration operator in Golang

A variable is a storage location or place holder used for holding the value. It allows us to manipulate and retrieve the stored information. There are two ways to declare the variables in Golan which are as follows:

  • Using var keyword
  • Using a short declaration operator (:=)

Difference between var keyword and short declaration operator

var keyword short declaration operator
var is a lexical keyword present in Golang. := is known as the short declaration operator.
It is used to declare and initialize the variables inside and outside the functions. It is used to declare and initialize the variables only inside the functions.
Using this, variables have generally package level or global level scope. It can also have local scope. Here, variables has only local scope as they are only declared inside functions.
Declaration and initialzation of the variables can be done separately. Declaration and initialzation of the variables must be done at the same time.
It is optional to put type along with the variable declaration. There is no need to put type. If you, it will give error.

Example 1: In this program you can see the myvariable1 is declared using var keyword and it has local scope. myvariable2 is also declared using var keyword along with type int but there is no initialization has been done. So it will take the default value of int type i.e. zero(you can see in the output). myvariable3 is declared and initialized using short variable declaration operator and it has local scope.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to show the use of var lexical 
// keyword and short declaration operator
package main
  
import (
    "fmt"
)
  
func main() {
  
// using var keyword to declare 
// and initialize the variable
var myvariable1 = 100
  
fmt.Println(myvariable1)
  
// using var keyword to declare 
// the variable along with type
var myvariable2 int
  
fmt.Println(myvariable2)
  
// using short variable declaration
myvariable3 := 200
  
fmt.Println(myvariable3)
      
}

chevron_right


Output:

100
0
200

Example 2: In this program you can see the myvariable1 is declared using var keyword and it has global scope. myvariable2 is also declared using var keyword along with type int but there is no initialization has been done. So it will take the default value of int type i.e. zero(you can see in the output). myvariable3 is declared and initialized using short variable declaration operator and it has local scope.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to show the use of var lexical 
// keyword and short declaration operator
package main
  
import (
    "fmt"
)
  
// using var keyword to declare 
// and initialize the variable
// it is package or you can say 
// global level scope
var myvariable1 = 100
  
func main() {
  
// accessing myvariable1 inside the function
fmt.Println(myvariable1)
  
// using var keyword to declare 
// the variable along with type
var myvariable2 int
  
fmt.Println(myvariable2)
  
// using short variable declaration
myvariable3 := 200
  
fmt.Println(myvariable3)
      
}

chevron_right


Output:

100
0
200

Example 3: In this program you can see the myvariable1 is declared using var keyword and it has global scope. myvariable2 is also declared using var keyword along with type int but there is no initialization has been done. So it will take the default value of int type i.e. zero(you can see in the output). myvariable3 is declared and initialized using short variable declaration operator outside the function which is not allowed and hence gives error.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to show the use of var lexical 
// keyword and short declaration operator
package main
  
import (
    "fmt"
)
  
// using var keyword to declare 
// and initialize the variable
// it is package or you can say 
// global level scope
var myvariable1 = 100
  
// using short variable declaration
// it will give an error as it is not 
// allowed outside the function
myvariable3 := 200
  
func main() {
  
// accessing myvariable1 inside the function
fmt.Println(myvariable1)
  
// using var keyword to declare 
// the variable along with type
var myvariable2 int
  
fmt.Println(myvariable2)
  
  
fmt.Println(myvariable3)
      
}

chevron_right


Error:

./prog.go:18:1: syntax error: non-declaration statement outside function body



My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksforGeeks

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.