Skip to content

Instantly share code, notes, and snippets.



Last active Apr 15, 2019
What would you like to do?
package main
import (
// adaptation of
func doPodThings() struct{} {
// uncomment the below sleep to toggle the error codepath
// time.Sleep(3 * time.Second)
return struct{}{}
func main() {
ctxDur := 2 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), ctxDur)
defer cancel()
ch := make(chan struct{})
go func() {
for {
select {
case <-ctx.Done():
// don't send error here. it'll be returned in the
// select in the main goroutine
case ch <- doPodThings():
// you return on the chan receive so you don't need a loop here
for {
select {
case <-ch:
// you would return the pod here
case <-ctx.Done():
// this is where you return the error
fmt.Println("oh noes! timeout after", ctxDur)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.