Created
          January 21, 2021 23:53 
        
      - 
      
- 
        Save takakd/e808232ff7328aaf1ff67fe325743a84 to your computer and use it in GitHub Desktop. 
    Golang channel sample code.
  
        
  
    
      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
    
  
  
    
  | package main | |
| import ( | |
| "fmt" | |
| ) | |
| func main() { | |
| ch1 := make(chan int) | |
| ch2 := make(chan int) | |
| go func() { | |
| ch1 <- 1 | |
| close(ch1) | |
| fmt.Println("closed ch1") | |
| }() | |
| go func() { | |
| fmt.Println("set ch2") | |
| ch2 <- 2 | |
| close(ch2) | |
| fmt.Println("closed ch2") | |
| }() | |
| for { | |
| select { | |
| case v, ok := <-ch1: | |
| fmt.Printf("ch1 %d %v\n", v, ok) | |
| if !ok { | |
| ch1 = nil | |
| } | |
| case v, ok := <-ch2: | |
| fmt.Printf("ch2 %d\n", v) | |
| if !ok { | |
| ch2 = nil | |
| } | |
| default: | |
| fmt.Println("wait") | |
| } | |
| if ch1 == nil && ch2 == nil { | |
| fmt.Println("all channel closed") | |
| break | |
| } | |
| } | |
| fmt.Println("done") | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment