Sitemap
Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Golang Quick Reference: Variables

3 min readNov 7, 2023

--

Variable Declaration (aka Normal Declaration)

Declaration

Variable declaration in Go involves specifying the type and optionally initializing the variable. Here’s an example:

var age int
age = 30

In this example: `var` is a keyword that tells the compiler that we are going to declare a variable. `age` is the name of this variable and ‘int’ is the type of the variable. The statement `age = 30` is called initialization. It assigns value 30 to variable `age`. If we don’t initialize the variable; the compiler assigns default value to the variable based on it’s type. In Go, default value of an ‘int’ type variable is zero.

Short Declaration

Short declaration allows you to declare and initialize a variable in a single line, with type inference:

name := "Alice"

The statement `name := “Alice”` is as good as two statements `var name string` followed by `name=“Alice”`. Please note the difference between short declaration and normal declaration syntax. Short declaration uses `:=` while normal declaration uses only `=` operator.

Multiple Variable Declaration

You can declare multiple variables of the same or different types in a single statement:

var a, b, c int
x, y := 1, 2

The above statements are as good as:

    var a int
var b int
var c int
var x int
x = 1
var y int
y = 2

Redeclaration

Variables can’t be redeclared within the same scope:

var score int
score := 42 // Error: no new variables on left side of :=

Sample Program (Variable Declaration Types)

package main

import "fmt"

func main() {
// Declaration
var age int
age = 30

// Short Declaration
name := "Alice"

// Multiple Variable Declaration
var a, b, c int
x, y := 1, 2

fmt.Println("Age:", age)
fmt.Println("Name:", name)
fmt.Println("a, b, c:", a, b, c)
fmt.Println("x, y:", x, y)
}

Try out this code on Go playground: https://go.dev/play/p/vtNaLFYuKRd

Rules for Variable Declaration & Naming

  • Variable names must start with a letter or underscore.
  • Subsequent characters can be letters, digits, or underscores.
  • Variable names are case-sensitive.
  • Avoid using Go’s reserved keywords (e.g., var, func, for) as variable names.
  • Use meaningful and descriptive names for clarity.

When to Use Normal Declaration

Use normal declaration when:

  • You want to declare a variable without initializing it immediately.
  • You need to declare variables with different types in the same statement.

When to Use Short Declaration

Use short declaration when:

  • You want to declare and initialize a variable in a single line.
  • You’re working with a new variable within a limited scope.

Shadowing Problem

Consider this example (shadow.go):

package main

import "fmt"

var count = 10 // Outer count

func main() {
count := 5 // Inner count, shadowing the outer count
fmt.Println("Inner Count:", count)
}

Try out this code on Go playground: https://go.dev/play/p/V0pNjrddYFA

Get Rahul Sid Patil’s stories in your inbox

Join Medium for free to get updates from this writer.

The output of this program will be:

$ go run shadow.go 
Count: 5

Why did we get the output as 5 and NOT 10? It’s because of the Shadowing problem. Shadowing occurs when a variable declared in an inner scope has the same name as a variable in an outer scope. It can lead to confusion.

How to Solve Shadowing Problem

The clean code solution for the shadowing problem is to use packages and scopes appropriately. I will explain about packages and scopes in my next Quick Reference article.

Happy coding!

Clap 10 times for support.

Note: This article is part of my Golang: Quick Reference series for budding gophers.

Connect with me

Press enter or click to view image in full size

Thank you for being a part of the community

Before you go:

--

--

Author of 'Let Us Go' • AI-Native Engineering Researcher • Technologist • Entrepreneur

Stackademic
Stackademic

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.