Skip to content

Commit

Permalink
Channel Ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya43 committed May 30, 2021
1 parent 3af6642 commit 7fd96f1
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 02-channels/05-channel-ownership/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import "fmt"

func main() {
// Create channel owner goroutine which return channel and
// writes data into channel and
// closes the channel when done.

consumer := func(ch <-chan int) {
// read values from channel
for v := range ch {
fmt.Printf("Received: %d\n", v)
}
fmt.Println("Done receiving!")
}

owner := func() <-chan int {
ch := make(chan int)

go func() {
defer close(ch)
ch <- 1
ch <- 2
ch <- 3
}()

return ch
}

ch := owner()
consumer(ch)
}

0 comments on commit 7fd96f1

Please sign in to comment.