4 ms·
Isn’t what the article suggests (with defer and then WriteString) technically a race condition? Is there no way that the closer can get called before WriteStrin
by spease 2y ago
Isn’t what the article suggests (with defer and then WriteString) technically a race condition? Is there no way that the closer can get called before WriteString executes?
- assbuttbuttass 2y agodefer gets executed at the end of the current function
- spease 2y agoThanks! Not a Go programmer, so I saw the parentheses after the cloaure def and assume it translated to “execute this on a goroutine in the background immediately”
- tomohawk 2y agoThis is the way. func doSomething() (err error) { var f *os.File f, err = os.Create("foo") if err != nil { return } defer func(){ if nil != f { f.Close() } }() _, err = f.Write([]byte("bar") if err != nil { return } err = f.Close() f = nil return } EDIT: fixed the bug