Imagine planning a vacation — you need to pack your belongings into suitcases to organize and carry them easily.
In programming, variables work much the same way. They act like containers or “boxes” that hold data, allowing you to store, organize, and use information whenever you need it. Just like suitcases make traveling more manageable, variables make programming more efficient and practical.
Declaring a Variable Using var
In Go, declaring variables with var is somewhat similar to how variables were declared in older versions of JavaScript (with var) or in .NET.
Here’s a breakdown of a simple variable declaration in Go:
var animal string = "Luna"
var is the keyword indicating that we are defining a variable
animal is the name of the variable
string is the type of the variable
= “Luna” assigns the initial value to the variable
Note: In Go, semicolons are not used at the end of statements
Declaring Multiple Variables at Once with var
Declaring Multiple Variables at Once with var
This is especially useful for package-level variables. The variables do not need to be of the same type, and each can have its initial value.
Here’s an example:
var (
<name1> <type1> = <value1>
<name2> <type2> = <value2>

<nameN> <typeN> = <valueN>
)
We’ve seen generic declarations; now let’s be more practical and declare information about a person (Alice) and her bank account.
package main

import (
  "fmt"
)

func main() {
  var (
      userName      string  = "Alice"
      userAge       int     = 30
      accountBalance float64 = 1234.56
      isActive      bool    = true
  )

  fmt.Println("User Name:", userName)
  fmt.Println("User Age:", userAge)
  fmt.Println("Account Balance:", accountBalance)
  fmt.Println("Is Active:", isActive)
}
Output:
// User Name: Alice
// User Age: 30
// Account Balance: 1234.56
// Is Active: true
Short Variable Declaration (:=)
We can use the := shorthand to make our declarations even shorter, as it eliminates the need for the var keyword.
This approach is popular in Go and is especially useful when we don’t want to specify a type, as the type is inferred from the initial value.
package main

import (
  "fmt"
)

func main() {
  dog := "Luna"

  fmt.Println(dog) // Luna
}
Declaring multiple variables with a short variable declaration
You can also declare multiple variables simultaneously with the shorthand :=. Each variable must have an initial value, and they must all be declared on the same line.
Syntax:
<var1>, <var2>, ..., <varN> := <val1>, <val2>, ..., <valN>
Example:
package main

import (
  "fmt"
)

func main() {
  userName, userAge, accountBalance, isActive := "Alice", 30, 1234.56, true

  fmt.Println("User Name:", userName)
  fmt.Println("User Age:", userAge)
  fmt.Println("Account Balance:", accountBalance)
  fmt.Println("Is Active:", isActive)
}
Changing the Value of a Variable
Variables hold data, but their value doesn’t need to remain the same throughout the app’s runtime.
You can change the value frequently as needed. To do this, use a similar notation to when you set the initial value: <variable> = <value>.
For example, a variable might initially hold the value Forest but later be changed to Mia.
package main

import (
  "fmt"
)

func main() {
  cat := "Forest"

  fmt.Println(cat) // Forest

  cat = "Mia"

  fmt.Println(cat) // Mia
}
Changing multiple values at once
Just like declaring multiple variables in one line, you can also update multiple variables simultaneously.
The syntax is similar:
<var1>, <var2>, ..., <varN> = <val1>, <val2>, ..., <valN>
Example:
package main

import (
  "fmt"
)

func main() {
  property, size := "Land", 100

  fmt.Println(property, size) // Land 100

  property, size = "House", 80

  fmt.Println(property, size) // House 80
}