Last active
July 15, 2019 09:18
-
-
Save iamgoangle/03c9b0e1eedc63672d43ab75c10f3fd6 to your computer and use it in GitHub Desktop.
GO Channel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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