Inspectopedia Help

'defer' in the loop

Reports defer statements inside loops.

Using defer in loops can lead to resource leaks or unpredictable execution order of statements.


func main() { for { field, err := db.Query("SELECT 1") if err != nil { // ... } defer field.Close() // ... } }

Calls of defer row.Close() inside the loop are not executed until the function completes its execution. Not at the end of each step of the for loop. Such implementation might lead to overflow of the function's stack and other issues.

Inspection Details

Available in:

GoLand 2023.3



Last modified: 13 July 2023