Skip to content

Instantly share code, notes, and snippets.

@iamgoangle
Last active July 15, 2019 09:18
Show Gist options
  • Select an option

  • Save iamgoangle/03c9b0e1eedc63672d43ab75c10f3fd6 to your computer and use it in GitHub Desktop.

Select an option

Save iamgoangle/03c9b0e1eedc63672d43ab75c10f3fd6 to your computer and use it in GitHub Desktop.
GO Channel
// demo aum
package main
import (
"fmt"
"time"
)
func receiveOnly(ch <-chan string) {
// ch <- "test" // it will error (send to receive-only type <-chan string)
fmt.Println("test from receive only => ", <-ch)
}
func sendOnly(ch chan<- string, v string) {
// fmt.Println("test from send only => ", <-ch)
ch <- v
}
func main() {
ch := make(chan string)
go sendOnly(ch, "one")
go sendOnly(ch, "two")
go receiveOnly(ch)
go func() {
for {
select {
case msg := <-ch:
fmt.Println(msg)
}
}
}()
time.Sleep(5 * time.Second)
close(ch)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment