Difference between var keyword and short declaration operator in Golang - GeeksforGeeks

11 Jun, 2020
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 (:=)
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 program to show the use of var lexical // keyword and short declaration operatorpackage main  import (    "fmt")  func main() {  // using var keyword to declare // and initialize the variablevar myvariable1 = 100  fmt.Println(myvariable1)  // using var keyword to declare // the variable along with typevar myvariable2 int  fmt.Println(myvariable2)  // using short variable declarationmyvariable3 := 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 program to show the use of var lexical // keyword and short declaration operatorpackage main  import (    "fmt")  // using var keyword to declare // and initialize the variable// it is package or you can say // global level scopevar myvariable1 = 100  func main() {  // accessing myvariable1 inside the functionfmt.Println(myvariable1)  // using var keyword to declare // the variable along with typevar myvariable2 int  fmt.Println(myvariable2)  // using short variable declarationmyvariable3 := 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 program to show the use of var lexical // keyword and short declaration operatorpackage main  import (    "fmt")  // using var keyword to declare // and initialize the variable// it is package or you can say // global level scopevar myvariable1 = 100  // using short variable declaration// it will give an error as it is not // allowed outside the functionmyvariable3 := 200  func main() {  // accessing myvariable1 inside the functionfmt.Println(myvariable1)  // using var keyword to declare // the variable along with typevar myvariable2 int  fmt.Println(myvariable2)    fmt.Println(myvariable3)      }
Error:
./prog.go:18:1: syntax error: non-declaration statement outside function body
Akanksha_Rai
shubham_singh
Go-Keywords
Go-Operators
Golang
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
6 Best Books to Learn Go Programming Language
Arrays in Go
strings.Replace() Function in Golang With Examples
fmt.Sprintf() Function in Golang With Examples
How to Split a String in Golang?
Slices in Golang
Golang Maps
Different Ways to Find the Type of Variable in Golang
Inheritance in GoLang
Interfaces in Golang