Variables in Golang

Learn how to declare and use variables in Go (Golang). This guide covers variable declaration methods including explicit types, type inference, short declarations (:=), multiple variable declarations, and declaration blocks. Understand Go's zero-value initialization and best practices for writing clean, readable Go code.

One of the guiding principles behind Go (Golang) is readability: code should be simple to scan and easy to understand. Let’s face it most of the time we spend as developers is reading other people’s code (often in legacy systems), not writing brand-new code.

Variables in Go are straightforward to declare and use. Also, by default, all variables are initialized to their “zero value”, which helps you avoid many “uninitialized variable” bugs. (Example: numbers become 0, booleans become false, strings become "", and reference types become nil.)

Variable Declaration

There are multiple ways to declare variables in Go. Below are the common styles, along with when to use each one.

Declare it with explicit type

Here, you explicitly specify the type. Go automatically initializes the variable to its zero value, and you can assign a value later when you have it.

Syntax: var <variable_name> <data_type>

var name string
var age int
var location string

Zero values (quick reference):
int0,
float640.0,
boolfalse,
string"",
slices/maps/pointers/channels/functions/interfaces → nil.

This method is used in the following scenarios:

  • Declare now, assign later: common when the value comes from input, I/O, or later computation.
  • When the zero value is a meaningful starting state (for example, counters in loops).
  • When assignment happens inside branches, but you need the variable outside the if/switch.
  • Package-level variables (globals) where := is not allowed.

Declare with type inference

Here, you don’t specify the type explicitly—Go infers it from the assigned value. This is not dynamic typing: once the type is inferred, it is fixed, and you cannot assign a different type later.

Syntax: var <variable_name> = <value>

var city = "Bangalore"    // inferred as string
var pi = 3.14159          // inferred as float64


This method is used in the following scenarios:

  • Package-level variables (you can initialize cleanly without repeating types).
  • When declaring in a var block for readability.
  • When you want the code to read naturally (“this name holds this value”) and type repetition feels noisy.
  • When reassigning an existing variable in the same scope (and you want to avoid accidental shadowing with :=).

Declare multiple variables

Go lets you declare multiple variables in a single statement, which is useful when the variables are related. This can improve readability and reduce repetition.

Syntax: var variable1, variable2, variable3 … <data_type>

var length, breadth, height int
var boneMass, muscleMass float64

You can also initialize multiple variables together (including different types) using inference:

var count, label = 10,"ten"

This method is used in the following scenarios:

  • Related values declared together (dimensions, config values, grouped counters, etc.).
  • Multiple variables of the same type to reduce repetition.
  • Grouping configuration-like values in a concise way.
  • When you want a single “declaration line” for closely related state.

Declaration block

A var block lets you declare multiple variables in a clean, grouped style. You can use explicit types, inference, or a mix of both. This is commonly used for configuration-style declarations and package-level variables.

var (
	appName    = "JyotishLabs"
	maxRetries int = 3
	debug      = false
)


This method is used in the following scenarios:

  • Reads like a config section.
  • Works well at package level (where := is not allowed).
  • Keeps related variables together and makes the file easier to scan.

Short variable declaration (:=)

Inside functions, Go provides a short syntax using :=. It declares the variable and infers the type at the same time. This is the most common style for local variables.

Syntax: <variable_name> := <value>

func main() {
	name := "Siva"      // string
	age := 34           // int
	pi := 3.14159       // float64

	// At least one variable on the left must be new in :=
	age, city := 35, "Bangalore" // age is reassigned, city is newly declared

	_ = name
	_ = age
	_ = pi
	_ = city
}

Important notes:

  • := works only inside functions (not at package level).
  • In a statement like a, b := ..., at least one of a or b must be a new variable in that scope.
  • Be careful about variable shadowing: using := inside inner blocks can accidentally create a new variable with the same name.

Common Go rule: unused variables

Go does not allow unused local variables—your code won’t compile if you declare a variable and never use it. If you intentionally want to ignore a value, use the blank identifier _.

value, err := doSomething()
_ = value // intentionally ignored
_ = err   // intentionally ignored

srnyapathi
srnyapathi
Articles: 41

One comment

Comments are closed.