Difference between var keyword and short declaration operator in Golang Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like 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 = valuevarName := valueDifference between var keyword and short declaration operatorvar keywordshort declaration operatorvar 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 Comment More infoAdvertise with us Next Article Go Programming Language (Introduction) A anshul_aggarwal Follow Improve Article Tags : Go Language Go-Keywords Go-Operators Golang Similar Reads Go Tutorial Go or you say Golang is a procedural and statically typed programming language having the syntax similar to C programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language and mainly used in Google 2 min read Go Programming Language (Introduction) Go is a procedural programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language. Programs are assembled by using packages, for efficient management of dependencies. This language also supports env 11 min read time.Sleep() Function in Golang With Examples In Go language, time packages supplies functionality for determining as well as viewing time. The Sleep() function in Go language is used to stop the latest go-routine for at least the stated duration d. And a negative or zero duration of sleep will cause this method to return instantly. Moreover, t 3 min read Golang Tutorial - Learn Go Programming Language This Golang tutorial provides you with all the insights into Go Language programming, Here we provide the basics, from how to install Golang to advanced concepts of Go programming with stable examples. So, if you are a professional and a beginner, this free Golang tutorial is the best place for your 10 min read Top 10 Golang Project Ideas with Source Code in 2025 Golang, or Go, a programming language was created by Google. It's widely used for building different kinds of applications like websites and cloud services. The fastest way to master this language is by building projects related to it. This article introduces 10 beginner-friendly to medium-difficult 8 min read Interfaces in Golang In Go, an interface is a type that lists methods without providing their code. You canât create an instance of an interface directly, but you can make a variable of the interface type to store any value that has the needed methods.Exampletype Shape interface { Area() float64 Perimeter() float64}In t 3 min read Top 10 Golang Frameworks in 2025 Golang (or Go) is an open-source compiled programming language that is used to build simple, systematic, and secure software. It was designed by Google in the year 2007 and has been readily adopted by developers all over the world due to its features like memory safety, structural typing, garbage co 9 min read Data Types in Go Data types specify the type of data that a valid Go variable can hold. In Go language, the type is divided into four categories which are as follows: Basic type: Numbers, strings, and booleans come under this category.Aggregate type: Array and structs come under this category.Reference type: Pointer 7 min read strings.Contains Function in Golang with Examples strings.Contains Function in Golang is used to check the given letters present in the given string or not. If the letter is present in the given string, then it will return true, otherwise, return false. Syntax:Â func Contains(str, substr string) bool Here, str is the original string and substr is t 2 min read fmt.Sprintf() Function in Golang With Examples In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Sprintf() function in Go language formats according to a format specifier and returns the resulting string. Moreover, this function is defined under the fmt package. Here, you 2 min read Like