Instead of closing at the end of the goroutine, use defer at the beginning