Chapter 7: Declare Variables

Declare Variables — so let’s zoom in purely on the declaration part itself.

Declaring a variable in Go means telling the compiler:

  • “Hey, I want to create a named storage spot in memory”
  • “This spot will hold values of a certain type”
  • “Optionally give it an initial value right away”

Go is very strict about declarations — no undeclared variables allowed, no unused variables allowed (compiler error!), and types are checked at compile time.

There are mainly 3 practical ways to declare variables in real Go code (2026 style). I’ll explain each one like we’re typing together in VS Code, with pros/cons, when to use which, and full runnable examples.

1. The Classic var Declaration (Explicit & Verbose)

This is the most “traditional” way — similar to C, Java, etc.

Syntax patterns:

Go

Real examples:

Go

When to prefer this style (2026 community advice):

  • At package level (outside any function) — := is not allowed there!
  • When declaring without an initial value (you plan to assign later)
  • In big var blocks to group configuration/options
  • When you want the type to be very visible for readability (rare nowadays)

2. Short Variable Declaration with := (The Go Favorite — Idiomatic!)

This is the most common way you’ll see in modern Go code.

Syntax:

Go

Important rules:

  • Only allowed inside functions (not at package level)
  • Must introduce at least one new variable (or compile error)
  • Type is always inferred — no need to write it

Real examples:

Go

When to prefer :=:

  • 80–90% of declarations inside functions
  • When you have an obvious initial value (especially from function calls)
  • For loop variables, error handling, quick locals
  • Makes code shorter and cleaner

Gotcha — you cannot do this at package level:

Go

3. Rare / Special Cases (You’ll See These Occasionally)

  • Type inference with var (allowed since early Go, but less common now):
Go
  • Declaring and ignoring (use _ for unused return values):
Go
  • Shadowing (re-declaring same name in inner scope — careful!):
Go

Quick Comparison Table (Choose Wisely!)

Style Syntax Example Allowed Location Type Written? Best For Idiomatic Level (2026)
Full var var x int = 42 Anywhere Yes Package level, no init value Medium
var with inference var x = 42 Anywhere No Grouping, package level Low–Medium
Short := x := 42 Only inside functions No Everyday function code, errors, loops Very High
Grouped var (…) var ( a=1; b=”hi” ) Anywhere Optional Related config/options High for grouping

Your Tiny Practice Task Right Now

Create a file declare.go and try all styles:

Go

Run go run declare.go

Any confusion left?

  • Package vs local declarations?
  • Why no unused variables?
  • How := works with multiple returns?
  • Pointer declaration? (var p *int vs p := &x)
  • Or ready for next topic: constants, functions, pointers?

Keep asking — you’re building a really solid foundation step by step! 💪🇮🇳 Let’s keep going! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *