Skip to content

Instantly share code, notes, and snippets.

@huantt
Created August 11, 2022 03:24
Show Gist options
  • Select an option

  • Save huantt/bc756b3ed8d0e0975576ce042a09f580 to your computer and use it in GitHub Desktop.

Select an option

Save huantt/bc756b3ed8d0e0975576ce042a09f580 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
)
func write(ch chan int) {
for i := 0; i < 4; i++ {
ch <- i
fmt.Println("successfully wrote", i, "to ch")
}
close(ch)
}
func main() {
// creates capacity of 2
ch := make(chan int, 2)
go write(ch)
time.Sleep(2 * time.Second)
for v := range ch {
fmt.Println("read value", v, "from ch")
time.Sleep(2 * time.Second)
}
}
@huantt

huantt commented Aug 11, 2022

Copy link
Copy Markdown
Author

Output

successfully wrote 0 to ch
successfully wrote 1 to ch
read value 0 from ch
successfully wrote 2 to ch
read value 1 from ch
successfully wrote 3 to ch
read value 2 from ch
read value 3 from ch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment