Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
In Go, there are multiple ways to declare variables. Two of the most commonly used methods are the var
keyword and the short declaration operator (:=
). Let's explore the differences and usages of both in this tutorial.
var
KeywordThe var
keyword is a classic way to declare variables in Go. It's versatile, allowing you to specify the type of the variable, or omit it and let Go infer the type.
var x int var y string = "Hello"
var a = 42 var b = "Gopher"
In this case, a
is inferred to be of type int
, and b
is inferred to be of type string
.
var c, d int = 3, 4 var e, f = "Go", "lang"
:=
)The short declaration operator is a more concise way to declare and initialize variables. It can only be used within a function body.
func main() { x := 10 y := "Gopher" fmt.Println(x, y) }
Here, x
is inferred to be of type int
, and y
is inferred to be of type string
.
func main() { a, b := 20, "Hello" fmt.Println(a, b) }
var
keyword can be used for both local and package-level variables.:=
operator is restricted to function-level scope.:=
is new. This can be useful in multiple assignment scenarios.var
keyword, re-declaring a variable in the same block will result in a compile-time error.var
keyword, without an explicit initial value, are given their zero value (e.g., 0
for numerics, false
for booleans, and ""
for strings).var
keyword is more verbose but offers more flexibility, especially when declaring package-level variables or when a variable's type needs to be explicitly specified.Both the var
keyword and the short declaration operator have their places in Go code. The choice between them depends on the context and the specific needs of the code segment you're working on. The short declaration operator (:=
) offers concise syntax and is excellent for local variables, while the var
keyword provides more flexibility and is suitable for various scopes.
When to Use var
in Golang:
var
for explicit variable declaration when you need to specify the type or when not using the short declaration syntax.package main import "fmt" var globalVar int func main() { var localVar string fmt.Println(globalVar, localVar) }
Short Declaration Operator in Golang Explained:
:=
) is used for concise variable declaration and assignment.package main import "fmt" func main() { message := "Hello, Golang!" fmt.Println(message) }
Implicit Type Inference with Short Declaration in Golang:
package main import "fmt" func main() { age := 25 // Inferred as int name := "Alice" // Inferred as string fmt.Println(age, name) }
Explicit Type Specification with var
in Golang:
var
when you want to explicitly specify the variable type.package main import "fmt" var explicitVar float64 func main() { explicitVar = 3.14 fmt.Println(explicitVar) }
Golang Variable Declaration Examples:
var
and short declaration.package main import "fmt" var globalVar int func main() { var localVar int localVar = 42 message := "Hello, Golang!" fmt.Println(globalVar, localVar, message) }
Using var
vs :=
for Variable Initialization in Golang:
var
when you need to declare a variable without immediately assigning a value or when specifying the type.:=
for concise variable initialization with implicit type inference.package main import "fmt" var globalVar int // Declaration with var func main() { localVar := 42 // Short declaration for initialization fmt.Println(globalVar, localVar) }