The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
Yooloo

Golang 怎么友好的关闭一个 chan?

  •  
  •   Yooloo · Oct 20, 2019 · 4265 views
    This topic created in 2425 days ago, the information mentioned may be changed or developed.

    RT 友好又是怎么定义的呢?(纯小白)

    8 replies    2019-10-21 12:15:34 +08:00
    ClericPy
        1
    ClericPy  
       Oct 21, 2019   ❤️ 1
    我还以为你要问怎么用 select + timeout 的情况下优雅停止 goroutine...

    还真没去想过 chan 要我关...
    ClericPy
        2
    ClericPy  
       Oct 21, 2019
    搜了一把, 还真有优雅关闭 chan 的情况, 长见识了
    wangking
        3
    wangking  
       Oct 21, 2019
    要考虑的情景太多了,看的脑壳疼
    catror
        4
    catror  
       Oct 21, 2019 via Android   ❤️ 2
    1. 只由 sender 来关闭
    2. 一般不考虑关闭,除了一种情况:receiver 必须知道 sender 已经停止发送了
    javapythongo
        5
    javapythongo  
       Oct 21, 2019
    感觉一般不太需要关闭
    reus
        6
    reus  
       Oct 21, 2019
    谁发送谁关闭
    kid1412621
        7
    kid1412621  
       Oct 21, 2019
    我还以为你说的是 4chan
    reus
        8
    reus  
       Oct 21, 2019   ❤️ 1
    如果有多个发送者,就用一个 sync.WaitGroup,每次增加发送者时 Add,发送者结束时 Done,最后在需要关闭的时候 Wait 完再 close。通知发送者结束可以用 context.Context.Done
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3828 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 59ms · UTC 04:36 · PVG 12:36 · LAX 21:36 · JFK 00:36
    ♥ Do have faith in what you're doing.