Skip to content

Instantly share code, notes, and snippets.

@elisarver
Created February 23, 2016 01:01
Show Gist options
  • Save elisarver/4f3d9b4e2786feb348c9 to your computer and use it in GitHub Desktop.
Save elisarver/4f3d9b4e2786feb348c9 to your computer and use it in GitHub Desktop.
From Errors are Values (https://blog.golang.org/errors-are-values) – a pretty fantastic use of closures. Each of those writes passes along the result of the write to the next command and exits *before* trying the next step. This may solve an issue I've been having in several languages.
var err error
write := func(buf []byte) {
if err != nil {
return
}
_, err = w.Write(buf)
}
write(p0[a:b])
write(p1[c:d])
write(p2[e:f])
// and so on
if err != nil {
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment