Skip to content

Instantly share code, notes, and snippets.

@tzookb
Last active April 8, 2021 18:52
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 tzookb/ed099ad925112803150d5cf77742bea0 to your computer and use it in GitHub Desktop.
Save tzookb/ed099ad925112803150d5cf77742bea0 to your computer and use it in GitHub Desktop.
run go routines in a loop to get all the values
package main
import "fmt"
type Person struct {
name string
}
func onbg(theChannel chan Person, theName string) {
fmt.Println(theName)
p := Person{theName}
theChannel <- p
}
func main() {
names := []string{"tzook", "shavit", "romi", "dana", "arik"}
theChannel := make(chan Person, len(names))
for _, name := range names {
go onbg(theChannel, name)
}
n := 0
for n < len(names) {
thePerson := <-theChannel
fmt.Println(thePerson)
n += 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment