Skip to content

Instantly share code, notes, and snippets.

@tolitius
Created October 17, 2017 04:19
Show Gist options
  • Save tolitius/7c8c070ade37a7d3ce093da5fbe34af3 to your computer and use it in GitHub Desktop.
Save tolitius/7c8c070ade37a7d3ce093da5fbe34af3 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"time"
)
func consume(ctx context.Context, partition int) {
for {
select {
case <-ctx.Done():
fmt.Println("I am done")
break
default:
fmt.Printf("consuming from parition: %d\n", partition)
time.Sleep(1 * time.Second)
}
}
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
for i := 0; i < 5; i++ {
go consume(ctx, i)
}
time.Sleep(5 * time.Second)
cancel()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment