gitee.com/quant1x/gox@v1.21.2/api/reflect_array_test.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"reflect"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  type T1 struct {
    12  	f1     string "f one"
    13  	f2     string
    14  	f3     string `f three`
    15  	f4, f5 int64  `f four and five`
    16  }
    17  
    18  type T2 struct {
    19  	F1 string `array:"0"`
    20  	F2 string `array:"2"`
    21  }
    22  
    23  func Test_main(t *testing.T) {
    24  	t0 := T1{f1: "1", f2: "2", f3: "3", f4: 4, f5: 5}
    25  	t1 := reflect.TypeOf(t0)
    26  	f1, _ := t1.FieldByName("f1")
    27  	fmt.Println(f1.Tag) // f one
    28  	f4, _ := t1.FieldByName("f4")
    29  	fmt.Println(f4.Tag) // f four and five
    30  	f5, _ := t1.FieldByName("f5")
    31  	fmt.Println(f5.Tag) // f four and five
    32  
    33  	ts, err := json.Marshal(t0)
    34  	if err != nil {
    35  		t.Error(err)
    36  	} else {
    37  		var t2 T1
    38  		err = json.Unmarshal(ts, &t2)
    39  		if err != nil {
    40  			t.Error(err)
    41  		}
    42  	}
    43  
    44  	fmt.Println(ts)
    45  }
    46  
    47  func TestConvert(t *testing.T) {
    48  	str := "a0,a1,a2,a3,a4,a5"
    49  	sa := strings.Split(str, ",")
    50  	var t2 T2
    51  	_ = Convert(sa, &t2)
    52  	fmt.Println(t2)
    53  }