golang: defer inside a for loop
So I have an infinite for loop however I want to use defer to make my intention clearer. Problem is this defer would never run because my function wouldn’t return. This, in turn, would give you memory leak. As neat as it could be, you can use a closure/anonymous function for this. for x := 0; x < 2; x++ { func() { defer fmt.Println(x) }() } Real world use case: So what I’m trying to do is to have an infinite polling using context on AWS SQS to add a timeout if something is wrong with SQS(this can be anything from network errors to aws being down, it can happen)....