I have a service function in Golang where running an endless for loop. I wanted to get data from this function without return. What is the best solution, a channel, or io.Writer? The function and where I call it in a different package, because the funcion in package something
while where I call it is the main. There is an example with channels:
func Check(dst string, w chan string) bool {
for {
w <- data
}
return false
}
On the otherside where I call this function:
var wg sync.WaitGroup
func main() {
messages := make(chan string, 10)
wg.Add(3)
go checking("10.1.1.1", messages)
msg := <-messages
fmt.Println(msg)
wg.Wait()
}
func checking(ip string, msg chan string) {
defer wg.Done()
w := worker.ContainerAliveIndicator{}
w.Check(ip, msg)
}
In this case I only get the first message what the function take to the channel.