// This example demonstrates how to use Unmarshal to decode XDR encoded data from // a byte slice into a struct. func ExampleUnmarshal() { // Hypothetical image header format. type ImageHeader struct { Signature [3]byte Version uint32 IsGrayscale bool NumSections uint32 } // XDR encoded data described by the above structure. Typically this would // be read from a file or across the network, but use a manual byte array // here as an example. encodedData := []byte{ 0xAB, 0xCD, 0xEF, 0x00, // Signature 0x00, 0x00, 0x00, 0x02, // Version 0x00, 0x00, 0x00, 0x01, // IsGrayscale 0x00, 0x00, 0x00, 0x0A} // NumSections // Declare a variable to provide Unmarshal with a concrete type and instance // to decode into. var h ImageHeader remainingBytes, err := xdr.Unmarshal(encodedData, &h) if err != nil { fmt.Println(err) return } fmt.Println("remainingBytes:", remainingBytes) fmt.Printf("h: %+v", h) // Output: // remainingBytes: [] // h: {Signature:[171 205 239] Version:2 IsGrayscale:true NumSections:10} }
func BenchmarkReflUnmarshal(b *testing.B) { var t XDRBenchStruct for i := 0; i < b.N; i++ { _, err := refl.Unmarshal(e, &t) if err != nil { b.Fatal(err) } } }
func BenchmarkUnmarshal(b *testing.B) { b.StopTimer() // Hypothetical image header format. type ImageHeader struct { Signature [3]byte Version uint32 IsGrayscale bool NumSections uint32 } // XDR encoded data described by the above structure. encodedData := []byte{ 0xAB, 0xCD, 0xEF, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0A} var h ImageHeader b.StartTimer() for i := 0; i < b.N; i++ { _, _ = xdr.Unmarshal(encodedData, &h) } b.SetBytes(int64(len(encodedData))) }
func (x XdrSerializer) Unmarshal(d []byte, o interface{}) error { _, err := xdr.Unmarshal(d, o) return err }