Return data from goroutines
examples/return-data-from-goroutines/return_data_from_goroutines.go
package main import ( "fmt" "sync" ) func main() { fmt.Println("Start") var wg sync.WaitGroup wg.Add(1) go func() { res := count(5, "Apple") fmt.Printf("Apple: %v\n", res) wg.Done() }() wg.Wait() fmt.Println("End") } func count(n int, name string) int { sum := 0 for i := 1; i <= n; i++ { fmt.Printf("%v %v\n", name, i) sum += i } return sum }
Start Apple 1 Apple 2 Apple 3 Apple 4 Apple 5 Apple: 15 End