Today I improved my Go Conference Booking App by learning how Go handles collections and loops. I explored arrays, slices, infinite loops, range, strings.Fields(), and the blank identifier.
Here’s what I learned:
Arrays (Fixed Size Collections)
An array in Go has a fixed size.
Example:
var numbers [3]int = [3]int{10, 20, 30}
- Fixed length
If I used an array for bookings, it would look like this:
var bookings [50]string
But this is limiting because:
- The size is fixed
- Harder to manage dynamic data
That’s why slices are usually preferred.
Slices (Dynamic Collections)
Instead of using a fixed array, I used a slice to store bookings:
bookings := []string{}
Every time a user books:
bookings = append(bookings, fName+" "+lName)
What I Learned:
- Slices don’t have a fixed size
-
append()adds new elements - Slices are the most common way to manage lists in Go
Infinite Loop
I wrapped the booking logic inside an infinite loop:
for {
// booking logic here
}
Why?
- Allows multiple users to book tickets continuously
- The program keeps running until manually stopped
Looping with range (For-Each)
To extract first names from all bookings:
for _, booking := range bookings {
name := strings.Fields(booking)
firstNames = append(firstNames, name[0])
}
What range Does:
- Iterates over slices
- Returns both index and value
- Makes looping clean and simple
Blank Identifier (_)
In this loop:
for _, booking := range bookings
-
_ignores the index - We only needed the value (booking)
- Prevents "unused variable" errors
This is called the blank identifier in Go.
strings.Fields()
I used:
name := strings.Fields(booking)
What it does:
- Splits a string into a slice of words
- Automatically separates by spaces
Example:
"Kervie Jay" , "Kurt John"
Becomes:
["Kervie", "Kurt"]
That’s how I extracted the first name:
name[0]
Using uint
var remainingTickets uint = 50
var userTickets uint
What I Learned:
-
uintmeans unsigned integer (cannot be negative) - Perfect for values like ticket counts
Today I learned the difference between arrays and slices in Go, and why slices are better for handling dynamic data like user bookings. I practiced using loops, especially infinite loops and the range keyword, to iterate through slices and process stored values. I also learned how to use strings.Fields() to split strings and the blank identifier _ to ignore unused variables in a loop.
Top comments (0)