Variables
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" 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 :=
:= 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.
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
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
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
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.
| JavaScript | Go | Notes |
|---|---|---|
| let x = 5; | x := 5 | Type inferred automatically in both |
| let x; // undefined | var x int // 0 | Go has no "undefined" — zero values fill the gap |
| x can later hold a string | x cannot — fixed type forever | Static typing is the core difference |
| const for "shouldn't reassign" | No direct equivalent for variables | Go's const is for fixed, compile-time-known values only |
Coding Challenges
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 solutionWrite 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 solutionWrite 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 solutionChapter 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