From 76cf6bac07a8188d99788d76a12774d0f9f5e3ec Mon Sep 17 00:00:00 2001
From: Rob Pike
This works, but there is a bug. If the call to os.Create fails, the
function will return without closing the source file. This can be easily
-remedied by putting a call to src.Close() before the second return statement,
+remedied by putting a call to src.Close before the second return statement,
but if the function were more complex the problem might not be so easily
noticed and resolved. By introducing defer statements we can ensure that the
files are always closed:
@@ -160,7 +160,8 @@ For a real-world example of panic and recover, see the
It decodes JSON-encoded data with a set of recursive functions.
When malformed JSON is encountered, the parser calls panic to unwind the
stack to the top-level function call, which recovers from the panic and returns
-an appropriate error value (see the 'error' and 'unmarshal' functions in
+an appropriate error value (see the 'error' and 'unmarshal' methods of
+the decodeState type in
decode.go).
-Other uses of defer (beyond the file.Close() example given earlier) +Other uses of defer (beyond the file.Close example given earlier) include releasing a mutex: