Skip to content

Instantly share code, notes, and snippets.

@kevinetienne
Created November 6, 2018 09:55
Show Gist options
  • Save kevinetienne/ed10b7f425df9c47d221b578115b263e to your computer and use it in GitHub Desktop.
Save kevinetienne/ed10b7f425df9c47d221b578115b263e to your computer and use it in GitHub Desktop.
Infinite loop to get data from a source
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
dataChan := make(chan string)
go func() {
for {
dataChan <- fmt.Sprintf("data %f", rand.Int())
time.Sleep(time.Second)
}
}()
afterCh := time.After(5 * time.Second)
ll := []string{}
for {
select {
case l := <-dataChan:
fmt.Println("Got:", l)
ll = append(ll, l)
case <-afterCh:
fmt.Println("Do some processing with %v", ll)
afterCh = time.After(5 * time.Second)
ll = []string{}
}
}
}
@henrahmagix
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment