🔧 Programmierung 🕛 vor 6 Monaten 2 Min Lesezeit
0

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

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

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:




CODE
var numbers [3]int = [3]int{10, 20, 30}







  • Fixed length



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




CODE
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:




CODE
bookings := []string{}






Every time a user books:




CODE
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:




CODE
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:




CODE
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:




CODE
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:




CODE
name := strings.Fields(booking)






What it does:




  • Splits a string into a slice of words

  • Automatically separates by spaces



Example:




CODE
"Kervie Jay" , "Kurt John"






Becomes:




CODE
["Kervie", "Kurt"]






That’s how I extracted the first name:




CODE
name[0]









Using uint






CODE
var remainingTickets uint = 50
var userTickets uint






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.

Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
9 Quellen
CVE-2022-44169 | Tenda AC15 15.03.05.18 formSetVirtualSer buffer overflow (EUVD-2022-47119)
1 Quelle
Best early October Prime Day deals: Save on TVs, smartwatches, and more tech
1 Quelle
I gave Claude Code $100 and 30 days to make a profit. Day 1, it built a product. Here's the pattern it used.
Ähnliche Beiträge
🔍 Verwandte News

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

Thematisch verwandte Begriffe: Learning, Notes, Part, Array · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...