Multiple goroutines listening on one channel

前端 未结 5 1552
攒了一身酷
攒了一身酷 2020-11-28 19:49

I have multiple goroutines trying to receive on the same channel simultaneously. It seems like the last goroutine that starts receiving on the channel gets the value. Is thi

5条回答
  •  暖寄归人
    2020-11-28 20:24

    I've studied existing solutions and created simple broadcast library https://github.com/grafov/bcast.

        group := bcast.NewGroup() // you created the broadcast group
        go bcast.Broadcasting(0) // the group accepts messages and broadcast it to all members
    
        member := group.Join() // then you join member(s) from other goroutine(s)
        member.Send("test message") // or send messages of any type to the group 
    
        member1 := group.Join() // then you join member(s) from other goroutine(s)
        val := member1.Recv() // and for example listen for messages
    

提交回复
热议问题