Skip to content

Instantly share code, notes, and snippets.

@mitsuse
Created February 15, 2016 15:52
Show Gist options
  • Save mitsuse/3c57a996d72df5d695db to your computer and use it in GitHub Desktop.
Save mitsuse/3c57a996d72df5d695db to your computer and use it in GitHub Desktop.
Simplify error checking of `binary.Read` by iterating over a slice of pointers.
package main
import (
"bytes"
"encoding/binary"
"fmt"
"os"
)
func main() {
var b1, b2, b3 byte
data := []interface{}{&b1, &b2, &b3}
expectation := []byte{1, 2, 3}
r := bytes.NewReader(expectation)
for _, d := range data {
if err := binary.Read(r, binary.BigEndian, d); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
}
result := []byte{b1, b2, b3}
fmt.Println(bytes.Compare(result, expectation) == 0)
}
@mitsuse
Copy link
Author

mitsuse commented Feb 15, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment