Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- )
- func total(ch chan int) {
- res := 0
- for iter := range ch {
- res += iter
- }
- ch <- res
- }
- func main() {
- ch := make(chan int)
- go total(ch)
- ch <- 1
- ch <- 2
- ch <- 3
- fmt.Println("Total is ", <-ch)
- }
- throw: all goroutines are asleep - deadlock!
- package main
- import (
- "fmt"
- )
- func total(in chan int, out chan int) {
- res := 0
- for iter := range in {
- res += iter
- }
- out <- res // sends back the result
- }
- func main() {
- ch := make(chan int)
- rch := make(chan int)
- go total(ch, rch)
- ch <- 1
- ch <- 2
- ch <- 3
- close (ch) // this will end the loop in the total function
- result := <- rch // waits for total to give the result
- fmt.Println("Total is ", result)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement