Skip to content

Instantly share code, notes, and snippets.

@chiiph

chiiph/defer.go

Created Dec 26, 2012
Embed
What would you like to do?
Defer example in Go
func CopyFile(dstName, srcName string) (written int64, err error) {
src, err := os.Open(srcName)
if err != nil {
return
}
defer src.Close()
dst, err := os.Create(dstName)
if err != nil {
return
}
defer dst.Close()
return io.Copy(dst, src)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.