Created
November 6, 2018 09:55
-
-
Save kevinetienne/ed10b7f425df9c47d221b578115b263e to your computer and use it in GitHub Desktop.
Infinite loop to get data from a source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
commented
Nov 6, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment