Skip to content

Latest commit

 

History

History
56 lines (42 loc) · 952 Bytes

do-something-n-times.md

File metadata and controls

56 lines (42 loc) · 952 Bytes

Do Something N Times

With Go 1.23 there is a new for-range syntax that makes looping a bit easier and more compact.

Instead of needing to set up our 3-part for-loop syntax, we can say we want to do something N times with for range N.

for range n {
	// do something
}

Let's look at an actual, runnable example:

package main

import "fmt"
import "math/rand"
import "time"

func main() {
	rand.Seed(time.Now().UnixNano()) 

	food := []string{"taco", "burrito", "torta", "enchilada", "tostada"}

	for range 5 {
		randomIndex := rand.Intn(len(food))
		fmt.Println(food[randomIndex])
	}
}

The output is random and might look something like this:

$ go run loop.go
taco
burrito
tostada
taco
enchilada

I appreciate this syntax addition because it feels very akin to Ruby's #times method:

5.times do
  # do something
end

source