Open In App

Difference between var keyword and short declaration operator in Golang

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:

  • var varName type = value
  • varName := value

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 initialization of the variables can be done separately. Declaration and initialization of the variables must be done at the same time.
It is mandatory 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. 

Go




// 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
// type is mandatory while declaration
var myvariable2 int
 
fmt.Println(myvariable2)
 
// using short variable declaration
myvariable3 := 200
 
fmt.Println(myvariable3)
     
}


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. 

Go




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


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. 

Go




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


Error:

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



Last Updated : 11 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads