mirror of
https://github.com/golang/go
synced 2024-11-17 18:54:42 -07:00
encoding/binary: add example for Read multi
Change-Id: I27ff99aa7abb070f6ae79c8f964aa9bd6a83b89d Reviewed-on: https://go-review.googlesource.com/53730 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
590c5b0807
commit
812124a567
@ -51,6 +51,30 @@ func ExampleRead() {
|
||||
// Output: 3.141592653589793
|
||||
}
|
||||
|
||||
func ExampleRead_multi() {
|
||||
data := struct {
|
||||
PI float64
|
||||
Uate uint8
|
||||
Mine [3]byte
|
||||
Too uint16
|
||||
}{}
|
||||
b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, 0xff, 0x01, 0x02, 0x03, 0xbe, 0xef}
|
||||
buf := bytes.NewReader(b)
|
||||
err := binary.Read(buf, binary.LittleEndian, &data)
|
||||
if err != nil {
|
||||
fmt.Println("binary.Read failed:", err)
|
||||
}
|
||||
fmt.Println(data.PI)
|
||||
fmt.Println(data.Uate)
|
||||
fmt.Printf("% x\n", data.Mine)
|
||||
fmt.Println(data.Too)
|
||||
// Output:
|
||||
// 3.141592653589793
|
||||
// 255
|
||||
// 01 02 03
|
||||
// 61374
|
||||
}
|
||||
|
||||
func ExampleByteOrder_put() {
|
||||
b := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint16(b[0:], 0x03e8)
|
||||
|
Loading…
Reference in New Issue
Block a user