so my question is how to send message to channels that broadcast
function gets only if channel is not closed and just for once.
after sending message should increase sentNumber
.
I say just to remind, there is a time limit for sending message to all channels!
package main
import (
"fmt"
"sync"
"time"
)
var (
sentNumber int
)
func broadcast(waitTime time.Duration, message string, ch ...chan string) (sentNumber int) {
start := time.Now()
for _, channel := range ch {
if time.Since(start) >= waitTime {
break
}
go send(channel, message)
}
return 0
}
func send(channel chan string, message string) {
for {
if _,open := <-channel; open{
break
}
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Done()
channel <- message
}()
wg.Wait()
}
func main() {
a := make(chan string, 1)
b := make(chan string, 1)
broadcast(5, "secret message", a, b)
fmt.Println(<-a)
fmt.Println(<-b)
}