Go
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions and Methods
-
Section 4: Concurrency
-
Section 5: Packages and Tooling
-
Section 6: More Standard Library
-
Section 7: Building Services
-
Section 8: Advanced Go
-
Section 9: Go in the Cloud-Native Ecosystem
-
Section 10: Data Structures and Algorithms in Go
-
Section 11: Testing and Deployment
-
Section 12: Practical Projects
-
Section 13: More Standard Library Practice
-
Section 14: More Practice Projects
-
Section 15: Design Patterns in Go
-
Section 16: Interview Practice
-
Section 17: Package fmt In Depth
-
Section 18: Package strings and strconv
-
Section 19: Package os and io
-
Section 20: Package time
-
Section 21: Package sort and container
-
Section 22: Package encoding
-
Section 23: Package net/http In Depth
-
Section 24: Package context
-
Section 25: Package regexp and bytes
-
Section 26: Package errors In Depth
-
Section 27: Package crypto and hash
-
Section 28: Package flag and log
-
Section 29: Package sync In Depth
-
Section 30: More Practice Exercises
-
Section 31: Go Modules and Workspaces In Depth
-
Section 32: Generics Deep Dive (Go 1.18+)
-
Section 33: Testing Package In Depth
-
Section 34: More Interview and Whiteboard Practice
-
Section 35: Package math and unicode
-
Section 36: Package path and filepath
-
Section 37: Package database/sql
-
Section 38: Package text/template and html/template
-
Section 39: Package archive and compress
-
Section 40: Lower-Level net Package
-
Section 41: Package runtime and reflect
-
Section 42: Package embed and io/fs
-
Section 43: Go Assembly and CGO Basics
-
Section 44: Building CLIs and TUIs
-
Section 45: Go Performance Tuning
-
Section 46: More Real-World Projects
-
Section 47: Go in Production
-
Section 48: Go Design Patterns
-
Section 49: Go Interfaces Deep Dive
-
Section 50: Final Practice and Review
6: Variables and Types
Up until now, we've been playing with values directly. But in a real app, you need a way to hold onto data, change it, and ensure that the data is exactly what you expect it to be. That's where variables and types come in. Go is statically typed, which is a fancy way of saying the compiler wants to know exactly what kind of data is in every variable before the program even runs. It's a bit more strict than Python or JavaScript, but it saves you from a lot of midnight debugging sessions.
Mapping out our coffee order
Let's build a small snippet that handles a coffee shop order. We need to track a few different things: who the customer is, how many coffees they want, the price per cup, and whether they've already paid. Each of these requires a different type.
package main
import "fmt"
func main() {
var customerName string = "Alice"
var quantity int = 2
var pricePerCup float64 = 4.50
var isPaid bool = false
fmt.Printf("%s ordered %d coffees at $%.2f each. Paid: %v\n", customerName, quantity, pricePerCup, isPaid)
}
I used var here to be explicit. You'll see string for text, int for whole numbers, float64 for decimals (always use 64-bit floats in Go unless you have a very specific reason not to), and bool for true/false. It's clear, it's readable, and the compiler is happy.
Cutting the noise with short declarations
Now, if I'm honest, writing var name string = "value" every single time is exhausting. When you're inside a function, Go gives us a shorthand: the := operator. It tells Go to "infer" the type based on the value you provide.
// Instead of the long way, I can just do this:
customerName := "Bob"
quantity := 3
pricePerCup := 3.75
isPaid := true
This is what you'll see in 90% of professional Go code. It's cleaner. But remember, this only works inside functions. If you're declaring a variable at the package level (outside main), you have to use the var keyword.
Hitting the type mismatch wall
Here is where I usually trip up when I'm moving fast. Let's say I want to calculate the total cost of the order. I'll try to multiply the quantity (an int) by the price (a float64).
total := quantity * pricePerCup
fmt.Println("Total cost:", total)
If you try to run this, Go will scream at you. You'll see an error like: invalid operation: quantity * pricePerCup (mismatched types int and float64). In some languages, the computer just "figures it out" and converts the integer to a float for you. Go refuses to do that. It wants you to be intentional about how you handle your data to avoid precision errors.
Casting our way out of trouble
To fix this, I have to explicitly convert the int to a float64. This is called type conversion (or casting). I'll wrap the quantity variable in float64() to make it compatible with the price.
package main
import "fmt"
func main() {
quantity := 2
pricePerCup := 4.50
// I convert quantity to float64 so the types match
total := float64(quantity) * pricePerCup
fmt.Printf("Total cost: $%.2f\n", total)
}
Now it works perfectly. It feels like an extra step, but this strictness is exactly why Go programs are so stable. You can't accidentally mix types and end up with a weird rounding error in a financial calculation.
📋 Practical Task
Build a Flight Booking Summary
Create a program that manages a single flight booking. Your program should do the following:
- Declare a variable for the
passengerName(string). - Declare a variable for the
seatNumber(int). - Declare a variable for the
ticketPrice(float64). - Declare a variable for
hasBaggage(bool). - Declare a variable for a
baggageFee(float64) set to 25.00. - Calculate the
finalPrice. IfhasBaggageis true, add thebaggageFeeto theticketPrice. If false, the price remains the same. - Print a summary sentence including all the variables.
Challenge: Use short variable declarations (:=) for everything inside your main function.
There are no comments for now.