Variables
By the end of this lesson you'll be able to store text, numbers, and true/false values in Go, declare them three different ways, convert safely between types, and print them in clean output — the foundation of every Go program you'll write.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free Go course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
Strings use "double quotes" . Go also has byte , rune , and sized numbers like int64 , but the four above carry you through almost everything early on. Note Go writes true / false in lowercase .
1️⃣ Declaring Variables
Go gives you three ways to make a variable. var name type = value is the explicit form. You can drop the type and let Go infer it from the value. And inside a function the short declaration := does both at once — it's the form you'll reach for most. Use descriptive names like firstName or totalPrice ; code is read far more often than written. Read this worked example, run it, then you'll write your own.
Your turn. The program below is almost complete — fill in the three blanks marked ___ using the hints, then run it and check your output.
2️⃣ Zero Values & Multiple Assignment
In some languages a variable with no value is "undefined" and reading it is a bug. Go is safer: every type has a zero value it falls back to — 0 for numbers, "" for strings, false for bools. You can also declare several variables at once, which makes the classic two-line swap a single, clean line.
3️⃣ Constants with const
A constant is a value that must never change after it's set, and the Go compiler enforces that for you. Use const for fixed facts like a tax rate or a maximum limit — it documents your intent and stops accidental edits. Constants can live at package level (outside any function) or inside one. Note constants use = , never := .
4️⃣ Type Conversion
Go is statically typed and — unlike JavaScript or Python — has no implicit conversion . You can't add an int to a float64 directly; you convert one first. The syntax is the target type as a function: float64(x) , int(x) . Converting between numbers and text is different — that needs the strconv package ( strconv.Itoa for int→string, strconv.Atoi for string→int).
Now you try. Input from a user always arrives as string text, so before you can do maths you must convert it. Fill in the two blanks:
Zero values: int → 0, float64 → 0, string → "" (empty), bool → false.
Use := for the everyday case — a local variable inside a function whose type is obvious from its value. Use var when you need a package-level variable, want to state the type explicitly, or want a variable to start at its zero value with no initial value.
Q: Why won't my program compile because of an "unused" variable?
Go treats an unused local variable as a real error to keep code clean. Either use it, delete it, or assign it to the blank identifier _ if you genuinely need to ignore a value (common with the error from strconv.Atoi ).
Go never converts number types behind your back, because silent conversions hide bugs. Convert one side explicitly: float64(myInt) + myFloat . This is intentional strictness, not a missing feature.
Q: What's the difference between const and var ?
A var can be reassigned later; a const is fixed at compile time and can never change. Use const for fixed facts (a tax rate, a maximum) so the compiler stops accidental edits.
No blanks this time — just a brief and an outline to keep you on track. Declare the variables yourself, print a tidy profile, and check your output against the example in the comments. This is exactly the kind of small program real apps are made of.
Practice quiz
Which is the short variable declaration, valid only inside a function?
- var name = "Alice"
- var name string
- name := "Alice"
- let name = "Alice"
Answer: name := "Alice". The := form declares and assigns at once and works only inside functions.
What is the zero value of an int in Go?
- 0
- nil
- undefined
- -1
Answer: 0. Numbers default to 0; strings to ""; bools to false.
What is the zero value of a string?
- nil
- " "
- "null"
- "" (empty string)
Answer: "" (empty string). An unset string is the empty string, never undefined.
What is the zero value of a bool?
- true
- false
- 0
- nil
Answer: false. Bools default to false.
Why won't this compile: var x int with x never used?
- An unused local variable is a compile error
- x has no type
- int needs an initial value
- It compiles fine
Answer: An unused local variable is a compile error. Go treats an unused local variable as an error; assign to _ to discard.
How do you swap x and y in one line?
- swap(x, y)
- x = y; y = x
- x, y = y, x
- x, y := y, x
Answer: x, y = y, x. Multiple assignment swaps without a temp variable.
Which keyword declares a value that can never change?
- var
- const
- let
- final
Answer: const. const is fixed at compile time; the compiler rejects reassignment.
How do you convert an int total to a float64?
- total.(float64)
- float(total)
- (float64)total
- float64(total)
Answer: float64(total). The target type acts as a conversion function: float64(total).
Where does := fail to work?
- Inside main
- At package level (outside any function)
- Inside any function
- In a for loop
Answer: At package level (outside any function). At package level you must use var or const, not :=.
What does the blank identifier _ do?
- Declares a global
- Creates a constant
- Discards a value you intentionally ignore
- Marks a variable unused but kept
Answer: Discards a value you intentionally ignore. Assigning to _ discards a value, e.g. the error from strconv.Atoi.