gitee.com/quant1x/gox@v1.21.2/encoding/binary/cstruct/struct_test.go (about)

     1  package cstruct
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"unsafe"
     7  )
     8  
     9  type StructA struct {
    10  	A1 uint8
    11  	A2 uint32
    12  	A3 [5]uint8
    13  }
    14  
    15  type StructB struct {
    16  	B1 uint8
    17  	B2 StructA
    18  	B3 uint16
    19  	B4 float32
    20  	B5 [3]StructA
    21  }
    22  
    23  func TestStruct(t *testing.T) {
    24  	b := StructB{}
    25  	b.B1 = 127
    26  	b.B2.A1 = 56
    27  	b.B2.A2 = 999
    28  	b.B2.A3[0] = 0
    29  	b.B2.A3[1] = 1
    30  	b.B2.A3[2] = 2
    31  	b.B2.A3[3] = 3
    32  	b.B2.A3[4] = 4
    33  	b.B3 = 8888
    34  	b.B4 = 88.8
    35  	b.B5[0] = b.B2
    36  	b.B5[1] = b.B2
    37  	b.B5[2] = b.B2
    38  
    39  	data, _ := Pack(&b)
    40  
    41  	fmt.Println("len(b) =", unsafe.Sizeof(b))
    42  	fmt.Println("struct data len = ", len(data))
    43  	fmt.Println("struct data is:")
    44  	for i := 0; i < len(data); i++ {
    45  		fmt.Printf("%d ", data[i])
    46  	}
    47  }