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

女同事问了一个 golang 的技术问题,没答上来,溴大了

  •  
  •   nmap · Apr 27, 2021 · 7190 views
    This topic created in 2025 days ago, the information mentioned may be changed or developed.

    两个 goroutine:go1,go2
    go1 从 socket 读,读到的数据写到 ch1
    go2 从 ch2 读,读到的数据写到 socket
    以此来实现全双工通信

    问题:ch1 关闭之后,go1 不知道,一写就崩了,怎么避免?

    25 replies  •  2021-06-24 11:06:09 +08:00
    shakeyo
    shakeyo
    Apr 27, 2021
    1
    创建一个用于通知的 chan
    for 循环里监听这个 chan 是否有消息
    在关闭的时候写入
    在读取的时候监听
    有信号退出当前 channel 就行
    bfdh
    bfdh
    Apr 27, 2021❤️18
    2
    溴 ( xiù ) (英語:Bromine )是一種化學元素,其化學符號为 Br,原子序數为 35 —— 摘自维基百科
    hwdef
    hwdef
    Apr 27, 2021
    3
    从 ch 读的时候,可以检查是否已关闭了。
    brader
    brader
    Apr 27, 2021
    4
    这确实是个问题,需要今晚好好研究下
    masterclock
    masterclock
    Apr 27, 2021❤️7
    5
    chan 仅由写的一方关闭
    bruce00
    bruce00
    Apr 27, 2021
    6
    [How to Gracefully Close Channels]( https://go101.org/article/channel-closing.html)

    ```go
    func SafeSend(ch chan T, value T) (closed bool) {
    defer func() {
    if recover() != nil {
    closed = true
    }
    }()

    ch <- value // panic if ch is closed
    return false // <=> closed = false; return
    }
    ```
    keepeye
    keepeye
    Apr 27, 2021❤️1
    7
    ch1 由 go1 关闭,也就是永远由写的一方关闭 channel
    PinkPumpkin
    PinkPumpkin
    Apr 27, 2021 via Android❤️3
    8
    糗 qiǔ,溴 xiù
    juzisang
    juzisang
    Apr 27, 2021❤️2
    9
    秀大了
    linvon
    linvon
    Apr 27, 2021
    10
    go1 来关闭啊,怎么可能由消费者去关闭通道
    go2 用 for range 去读,也能感知到关闭
    echo1937
    echo1937
    Apr 27, 2021❤️5
    11
    你打标题的时候,又糗大了了一次。
    Latin
    Latin
    Apr 27, 2021
    12
    我怀疑你是套娃 给自己问问题
    name1991
    name1991
    Apr 27, 2021
    13
    女同事很会发现问题
    Presbyter
    Presbyter
    Apr 27, 2021
    14
    可以考虑用 context Cancel 控制一下
    go1(ctx){
    ...
    select{
    case <-ctx.Done():
    return
    ......
    }
    .....
    }
    SmartKeyerror
    SmartKeyerror
    Apr 27, 2021
    15
    The close built-in function closes a channel, which must be either bidirectional or send-only. It should be executed only by the sender, never the receiver, and has the effect of shutting down the channel after the last sent value is received.

    https://pkg.go.dev/builtin#close
    yogogo
    yogogo
    Apr 27, 2021
    16
    娶回家
    freemoon
    freemoon
    Apr 27, 2021
    17
    5 楼就是答案
    zhaohua
    zhaohua
    Apr 27, 2021
    18
    如果把问题简化为: 从关闭的 ch 中读取数据,如何才能不崩溃,答案是:做不到.
    2kCS5c0b0ITXE5k2
    2kCS5c0b0ITXE5k2
    Apr 27, 2021❤️1
    19
    你又可以发一贴 在 V2EX 发帖 认错字了 糗大了
    Vegetable
    Vegetable
    Apr 28, 2021
    20
    没人提但是我很好奇,panic 了就 recover 一下不也算方案吗
    yefuchao
    yefuchao
    Apr 28, 2021
    21
    https://tour.golang.org/concurrency/4

    A sender can close a channel to indicate that no more values will be sent. Receivers can test whether a channel has been closed by assigning a second parameter to the receive expression
    rekulas
    rekulas
    Apr 30, 2021
    22
    用指针应该有办法判断,但并不是好办法,最好还是听楼上的意见自己处理关闭
    iceteacover
    iceteacover
    May 14, 2021
    23
    我是来抖机灵的:

    不要把问技术问题女性当成女性。

    不抖机灵的:

    技术面前人人平等,请先尊重你同事的专业能力,再尊重性别。
    Lihanx9
    Lihanx9
    Jun 8, 2021
    24
    如果是针对这个场景,不要求必须使用 channel 的话。goroutine 里用 io.Copy 读写,根据返回值或者异常判断结束退出 goroutine,另外 io.Copy 用在 net.Conn 上的时候,底层会调用 sendfile,零拷贝效率可能还会高一点。这是我最近遇到这个问题用的方法,不知道对不对啊...
    TomatoCLI
    TomatoCLI
    Jun 24, 2021
    25
    @zhaohua 读取被关的 channel 不会崩。。。写才会
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1237 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 23:42 · PVG 07:42 · LAX 15:42 · JFK 18:42
    ♥ Do have faith in what you're doing.