DEV Community

Kervie Sazon
Kervie Sazon

Posted on

Go Learning Notes - Part 2: Array, Slices, Loops & String Processing

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}
Enter fullscreen mode Exit fullscreen mode
  • Fixed length

If I used an array for bookings, it would look like this:

var bookings [50]string
Enter fullscreen mode Exit fullscreen mode

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{}
Enter fullscreen mode Exit fullscreen mode

Every time a user books:

bookings = append(bookings, fName+" "+lName)
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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])
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • _ 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)
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Splits a string into a slice of words
  • Automatically separates by spaces

Example:

"Kervie Jay" , "Kurt John"
Enter fullscreen mode Exit fullscreen mode

Becomes:

["Kervie", "Kurt"]
Enter fullscreen mode Exit fullscreen mode

That’s how I extracted the first name:

name[0]
Enter fullscreen mode Exit fullscreen mode

Using uint

var remainingTickets uint = 50
var userTickets uint
Enter fullscreen mode Exit fullscreen mode

What I Learned:

  • uint means 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)