{{tag>Brouillon}} # Go lang - goroutines Voir : * https://gobyexample.com/timeouts * https://www.geeksforgeeks.org/go-language/select-statement-in-go-language/ ## Pb sortie de main avant terminaisons des goroutines - Attendre la fin de toutes les goroutines avant de terminer le programme (la fonction main) Voir : * https://stackoverflow.com/questions/18207772/how-to-wait-for-all-goroutines-to-finish-without-using-time-sleep * https://leapcell.medium.com/how-to-wait-for-multiple-goroutines-in-go-faf6dce827fc * https://groups.google.com/g/golang-nuts/c/mNhXnWLFOo4 * https://groups.google.com/g/golang-nuts/c/hIo_U_qwv84 * https://boldlygo.tech/archive/2025-08-06-waiting-for-goroutines/ * https://nathanleclaire.com/blog/2014/02/15/how-to-wait-for-all-goroutines-to-finish-executing-before-continuing/ * https://gobyexample.com/waitgroups * https://www.reddit.com/r/golang/comments/1y3spq/how_to_wait_for_all_goroutines_to_finish/ * https://codefinity.com/courses/v2/7f0f48cc-166e-439c-b067-a02fc91cc418/d1ac2dc4-039c-48a1-8392-e2746355d2da/ee461f59-ac15-467f-a700-229e75a8a0e5 * https://dev.to/neelp03/how-to-use-goroutines-for-concurrent-processing-in-go-34ph * https://pkg.go.dev/sync * https://medium.com/@hax.artisan/go-monitor-your-goroutine-application-9edbdd6e581b Solution naïve ~~~go import ( "runtime" "time" ) func wait_all_goroutines() { for runtime.NumGoroutine() > 1 { time.Sleep(500 * time.Millisecond) } } func main() { defer fmt.Println("FIN") // Last defer defer wait_all_goroutines() ch := make(chan int) go evenSum(1, 100, ch) } ~~~ ## Afficher les résultats des goroutine de la plus rapide à la plus longue Voir : * https://gobyexample.com/select ~~~go ch1 := make(chan int) ch2 := make(chan int) go evenSum(1, 10, ch1) go squareSum(1, 10, ch2) for range 2 { select { case r1 := <-ch1: fmt.Println(r1) case r2 := <-ch2: fmt.Println(r2) } } ~~~ FIXME