mirror of
https://github.com/jbranchaud/til
synced 2026-01-06 16:48:01 +00:00
Add Do Something N Times as a Go TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1496 TILs and counting..._
|
_1497 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -395,6 +395,7 @@ _1496 TILs and counting..._
|
|||||||
|
|
||||||
- [Access Go Docs Offline](go/access-go-docs-offline.md)
|
- [Access Go Docs Offline](go/access-go-docs-offline.md)
|
||||||
- [Build For A Specific OS And Architecture](go/build-for-a-specific-os-and-architecture.md)
|
- [Build For A Specific OS And Architecture](go/build-for-a-specific-os-and-architecture.md)
|
||||||
|
- [Do Something N Times](go/do-something-n-times.md)
|
||||||
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
|
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
|
||||||
- [Not So Random](go/not-so-random.md)
|
- [Not So Random](go/not-so-random.md)
|
||||||
- [Replace The Current Process With An External Command](go/replace-the-current-process-with-an-external-command.md)
|
- [Replace The Current Process With An External Command](go/replace-the-current-process-with-an-external-command.md)
|
||||||
|
|||||||
56
go/do-something-n-times.md
Normal file
56
go/do-something-n-times.md
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
# 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`.
|
||||||
|
|
||||||
|
```go
|
||||||
|
for range n {
|
||||||
|
// do something
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Let's look at an actual, runnable example:
|
||||||
|
|
||||||
|
```go
|
||||||
|
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:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ go run loop.go
|
||||||
|
taco
|
||||||
|
burrito
|
||||||
|
tostada
|
||||||
|
taco
|
||||||
|
enchilada
|
||||||
|
```
|
||||||
|
|
||||||
|
I appreciate this syntax addition because it feels very akin to Ruby's `#times`
|
||||||
|
method:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
5.times do
|
||||||
|
# do something
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
[source](https://eli.thegreenplace.net/2024/ranging-over-functions-in-go-123/)
|
||||||
Reference in New Issue
Block a user