Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

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. If hasBaggage is true, add the baggageFee to the ticketPrice. 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.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.