package main
import (
"fmt"
"time"
)
func receiver(ch <-chan int) {
time.Sleep(500 * time.Millisecond)
msg := <-ch
fmt.Printf("receive messages %d from the channel\n", msg)
}
func main() {
start := time.Now()
zero_buffer_ch := make(chan int, 0)
go receiver(zero_buffer_ch)
zero_buffer_ch <- 444
elapsed := time.Since(start)
fmt.Printf("Elapsed using zero_buffer channel: %v\n", elapsed)
restart := time.Now()
non_zero_buffer_ch := make(chan int, 1)
go receiver(non_zero_buffer_ch)
non_zero_buffer_ch <- 4444
reelapsed := time.Since(restart)
fmt.Printf("Elapsed using non zero_buffer channel: %v\n", reelapsed)
}
परिणाम:
चैनल से 444 संदेश प्राप्त करें
शून्य_बफ़र चैनल का उपयोग कर समाप्त: 505.6729ms
गैर शून्य_बफ़र चैनल का उपयोग कर समाप्त: 0s