Launches 10 Goroutines and each goroutine adding 10 values to a Channel
The program starts with 10 Goroutines. We created a ch string channel and writing data to that channel by running 10 goroutines concurrently. The direction of the arrow with respect to the channel specifies whether the data is sent or received. The arrow points towards ch specifies we are writing to channel ch. The arrow points outwards from ch specifies we are reading from channel ch.
Example
package main
import (
"fmt"
"strconv"
)
func main() {
ch := make(chan string)
for i := 0; i < 10; i++ {
go func(i int) {
for j := 0; j < 10; j++ {
ch <- "Goroutine : " + strconv.Itoa(i)
}
}(i)
}
for k := 1; k <= 100; k++ {
fmt.Println(k, <-ch)
}
}
1 Goroutine : 9
2 Goroutine : 9
3 Goroutine : 2
4 Goroutine : 0
5 Goroutine : 1
6 Goroutine : 5
7 Goroutine : 3
8 Goroutine : 4
9 Goroutine : 7
10 Goroutine : 6
....................
Most Helpful This Week
Golang program for implementation LZW Data Compression and Uncompression
Golang program for implementation of Interpolation Search
Program in Go language to Calculate Standard Deviation using Math package
How to set timeout for http.Get() requests in Golang?
How do you handle HTTP authentication with an HTTP client in Go?
Web Application to generate QR code in Golang