dimanche 19 janvier 2020

How I can update struct's array field value via reflect in Go?

I want to edit struct fields' value as interface{} parameters. I done almost, but when struct has array field, I couldn't modify that. I tried to find proper function with array value, but I couldn't find that. Here is my code.

edit_struct_via _interface.go

package main

import (
    "reflect"
    "fmt"
)


type myStruct struct {
    Name    [8]byte
    TID     [4]byte
    Chksum  uint16
}

func main() {
    var s = new(myStruct)
    name := []byte("Mike")
    tid  := []byte{0x01, 0x02, 0x03, 0x05}
    setArrayField(name, s.Name[:])
    setArrayField(tid, s.TID[:])
    s.Chksum = 0x0101

    myObj := reflect.ValueOf(s).Elem()

    typeOfMyObj := myObj.Type()
    for i := 0; i < myObj.NumField(); i++ {
        f := myObj.Field(i)
        fmt.Printf("%d: %s %s = %v\n", i,
            typeOfMyObj.Field(i).Name, f.Type(), f.Interface())
    }

    newName := []byte("Emil")
    myObj.Field(0).SetBytes(newName[:])
    // I trying to change array field at here, but panic occur here.
    // Because SetBytes must been called on slice, but myObj.Field(0) is array.

    myObj.Field(2).SetUint(99)             // It works.
    fmt.Println("s is now: ", *s)
}

func setArrayField(src []byte, target []byte) {
    // retun if src is bigger than target
    if len(src) > len(target){
        return
    }

    for index, item := range src {
        target[index] = item
    }
}

When I run above code, I get

0: Name [8]uint8 = [77 105 107 101 0 0 0 0]
1: TID [4]uint8 = [1 2 3 5]
2: Chksum uint16 = 257
panic: reflect: call of reflect.Value.SetBytes on array Value

goroutine 1 [running]:
reflect.flag.mustBe(...)
        /usr/local/go/src/reflect/value.go:208
reflect.Value.SetBytes(0x4a6180, 0xc000098010, 0x191, 0xc000098078, 0x4, 0x4)
        /usr/local/go/src/reflect/value.go:1557 +0x140
main.main()
        /home/hornet/go/src/gateway-golang/edit_struct_via _interface.go:33 +0x522
exit status 2

I'm using Ubuntu 18.04 and go1.13 linux/amd64





Aucun commentaire:

Enregistrer un commentaire