Last active
April 8, 2021 18:52
-
-
Save tzookb/ed099ad925112803150d5cf77742bea0 to your computer and use it in GitHub Desktop.
run go routines in a loop to get all the values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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