Skip to content

Instantly share code, notes, and snippets.

@taka011239
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save taka011239/11529137 to your computer and use it in GitHub Desktop.

Select an option

Save taka011239/11529137 to your computer and use it in GitHub Desktop.

Revisions

  1. taka011239 revised this gist May 5, 2014. 1 changed file with 25 additions and 0 deletions.
    25 changes: 25 additions & 0 deletions closeChReadRange.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    package main

    import (
    "fmt"
    "sync"
    )

    func main() {
    ch := make(chan int)
    var wg sync.WaitGroup

    go func() {
    wg.Add(1)
    defer wg.Done()
    for v := range ch {
    fmt.Println(v)
    }
    }()

    for i := 0; i < 5; i++ {
    ch <- i
    }
    close(ch)
    wg.Wait()
    }
  2. taka011239 revised this gist May 5, 2014. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion closeChRead.go
    Original file line number Diff line number Diff line change
    @@ -13,13 +13,18 @@ func main() {
    wg.Add(1)
    defer wg.Done()
    for {
    v := <-ch
    v, ok := <-ch
    if !ok {
    fmt.Println("channel is closed!")
    break
    }
    fmt.Println(v)
    }
    }()

    for i := 0; i < 5; i++ {
    ch <- i
    }
    close(ch)
    wg.Wait()
    }
  3. taka011239 created this gist May 5, 2014.
    25 changes: 25 additions & 0 deletions closeChRead.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    package main

    import (
    "fmt"
    "sync"
    )

    func main() {
    ch := make(chan int)
    var wg sync.WaitGroup

    go func() {
    wg.Add(1)
    defer wg.Done()
    for {
    v := <-ch
    fmt.Println(v)
    }
    }()

    for i := 0; i < 5; i++ {
    ch <- i
    }
    wg.Wait()
    }