Variables

Course 1 · Ch 2
Variables, Basic Types, and := vs var
Go is statically typed — every variable has a fixed type, decided either explicitly or by what's assigned to it

Chapter 1's message := "Compiled and ready" already used a variable without explaining it. JavaScript's let/const (any value, any type, can hold anything later) has no direct equivalent here — Go variables are statically typed: once a variable is created as a string, it can never later hold a number.

Declaring with var and an Explicit Type

var name string = "Philip" var age int = 35 var isStudent bool = true

var name string = "Philip" reads as: declare a variable called name, of type string, set to "Philip". The type is written explicitly between the name and the value — the opposite order to let, where no type appears at all.

The Short Declaration Operator :=

name := "Philip" // type "string" is inferred automatically age := 35 // type "int" is inferred isStudent := true // type "bool" is inferred

:= declares a new variable AND infers its type from the value on the right, in one step — far closer to JavaScript's const x = ... in everyday feel. It's the form used almost everywhere inside functions; var with an explicit type is mostly reserved for cases where no value is assigned yet, or the type genuinely needs to be different from what would be inferred.

:= only works for NEW variables
name := "Philip" followed later by another name := "Sam" in the same scope is a compile error — := declares, it doesn't just assign. To change an existing variable's value, drop the colon: name = "Sam".

Go's Core Basic Types

count := 42 // int — whole numbers price := 19.99 // float64 — decimal numbers label := "Hello" // string — text, always double-quoted active := true // bool — true or false, lowercase, no quotes

These four cover almost all everyday Go code: int, float64, string, bool. Unlike JavaScript's single general-purpose number type, Go separates whole numbers (int) from decimals (float64) — mixing them directly causes a compile error, covered next chapter.

Zero Values — Go Never Leaves a Variable Truly Empty

var count int // 0 — the "zero value" for int var label string // "" — empty string, the zero value for string var active bool // false — the zero value for bool

Declaring a variable with var and no value doesn't leave it as undefined the way JavaScript would — Go automatically gives it a sensible default for its type, called the zero value. There is no Go equivalent of undefined; every variable always holds a real, usable value of its type from the moment it's declared.

Declaring Several Variables Together

name, age := "Philip", 35 var ( width float64 = 12.5 height float64 = 4.0 )

Multiple variables can be declared on one line with :=, matching values to names left to right. The var ( ... ) block groups several explicit declarations together — useful for constants and configuration-style values declared at the top of a file, outside any function.

JavaScriptGoNotes
let x = 5;x := 5Type inferred automatically in both
let x; // undefinedvar x int // 0Go has no "undefined" — zero values fill the gap
x can later hold a stringx cannot — fixed type foreverStatic typing is the core difference
const for "shouldn't reassign"No direct equivalent for variablesGo's const is for fixed, compile-time-known values only

Coding Challenges

Challenge 1

Write a program that declares title (string), pages (int), and inStock (bool) using := for each, then prints all three using a single fmt.Println call.

📄 View solution
Challenge 2

Write a program that declares var total int with no value, prints it (confirming it's 0, not an error), then assigns it 100 using = (not :=) and prints it again.

📄 View solution
Challenge 3

Write a program that declares city, population := "London", 8900000 on one line, then deliberately tries city := "Paris" again further down in main(). Run it, note the compiler error, then fix it using = instead of := for the second assignment.

📄 View solution

Chapter 2 Quick Reference

  • var name type = value — explicit declaration with an explicit type
  • name := value — short declaration; type is inferred, only for NEW variables
  • name = value (no colon) — reassigns an EXISTING variable
  • Core types: int, float64, string, bool
  • Zero values: int → 0, float64 → 0, string → "", bool → false (no "undefined" in Go)
  • Static typing — a variable's type is fixed forever once declared
  • var ( ... ) block — group several explicit declarations together
  • Next chapter: operators, arithmetic, and Go's strict rules about mixing numeric types