github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/rlp/decode_tail_test.go (about)

     1  package rlp
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  )
     7  
     8  type structWithTail struct {
     9  	A, B uint
    10  	C    []uint `rlp:"tail"`
    11  }
    12  
    13  func ExampleDecode_structTagTail() {
    14  	// In this example, the "tail" struct tag is used to decode lists of
    15  	// differing length into a struct.
    16  	var val structWithTail
    17  
    18  	err := Decode(bytes.NewReader([]byte{0xC4, 0x01, 0x02, 0x03, 0x04}), &val)
    19  	fmt.Printf("with 4 elements: err=%v val=%v\n", err, val)
    20  
    21  	err = Decode(bytes.NewReader([]byte{0xC6, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06}), &val)
    22  	fmt.Printf("with 6 elements: err=%v val=%v\n", err, val)
    23  
    24  	// Note that at least two list elements must be present to
    25  	// fill fields A and B:
    26  	err = Decode(bytes.NewReader([]byte{0xC1, 0x01}), &val)
    27  	fmt.Printf("with 1 element: err=%q\n", err)
    28  
    29  	// Output:
    30  	// with 4 elements: err=<nil> val={1 2 [3 4]}
    31  	// with 6 elements: err=<nil> val={1 2 [3 4 5 6]}
    32  	// with 1 element: err="rlp: too few elements for rlp.structWithTail"
    33  }