Created
February 15, 2016 15:52
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Playground: http://play.golang.org/p/iyanSMTe4C