Skip to content

Instantly share code, notes, and snippets.

@sgnn7
Last active October 7, 2020 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgnn7/c869aa3e1904c10283785a0309c2cb7a to your computer and use it in GitHub Desktop.
Save sgnn7/c869aa3e1904c10283785a0309c2cb7a to your computer and use it in GitHub Desktop.
Simple async golang loop
package main
import (
"log"
"math"
"os"
"strconv"
"time"
)
func do_something(index int) {
for {
log.Println(index)
time.Sleep(1000 * time.Millisecond)
}
}
func main() {
log.Println("Starting...")
threads, _ := strconv.Atoi(os.Args[1])
log.Printf("Using %d threads...", threads)
for index := 0; index < threads; index++ {
go do_something(index + 1)
}
<-time.After(time.Duration(math.MaxInt64))
log.Println("Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment