如何给 reflect Field() 赋值?

How to assign value into reflect Field()?

我遇到过这样的问题。 如果字段的类型和名称相同,我需要比较两个结构。 将值从 sour 赋值给 dist。我写了一些代码,但在这里我可以分配 reflect.Field() 值。你可以帮帮我吗?我在波纹管中创建测试

import (
    "reflect"
    "testing"
)

func Assign(sour interface{}, dist interface{}) uint {
    counter := 0
    source  := reflect.ValueOf(sour)

    target  := reflect.ValueOf(dist)

    typeSource := reflect.TypeOf(sour)


    typeTarget := reflect.TypeOf(dist)
    for i:=0; i<source.NumField(); i++{
        for j:=0; j<target.NumField();j++{
            if (typeSource.Field(i).Type==typeTarget.Field(j).Type && typeSource.Field(i).Name==typeTarget.Field(j).Name){
                counter = counter + 1
                target.FieldByName(typeSource.Field(i).Name).Set(source.Field(i))


            }
        }
    }

    return uint(counter)
}

func TestAssign(t *testing.T) {
    type A struct {
        A string
        B uint
        C string
    }
    type B struct {
        AA string
        B  int
        C  string
    }
    var (
        a = A{
            A: "Тест A",
            B: 55,
            C: "Test C",
        }
        b = B{
            AA: "OKOK",
            B:  10,
            C:  "FAFA",
        }
    )
    result := Assign(a, b)
    switch true {
    case b.B != 10:
        t.Errorf("b.B = %d; need to be 10", b.B)
    case b.C != "Test C":
        t.Errorf("b.C = %v; need to be  'Test C'", b.C)
    case result != 1:
        t.Errorf("Assign(a,b) = %d; need to be 1", result)
    }
}

要使 Assign 起作用,第二个参数必须 可寻址,即您需要传递一个指向结构值的指针。

// the second argument MUST be a pointer to the struct
Assing(source, &target)

然后您需要稍微修改 Assign 的实现,因为指针没有字段。可以使用Elem()方法获取指针指向的struct值。

func Assign(sour interface{}, dist interface{}) uint {
    counter := 0
    source := reflect.ValueOf(sour)

    // dist is expected to be a pointer, so use Elem() to
    // get the type of the value to which the pointer points
    target := reflect.ValueOf(dist).Elem()

    typeSource := reflect.TypeOf(sour)

    typeTarget := target.Type()
    for i := 0; i < source.NumField(); i++ {
        for j := 0; j < target.NumField(); j++ {
            if typeSource.Field(i).Type == typeTarget.Field(j).Type && typeSource.Field(i).Name == typeTarget.Field(j).Name {
                counter = counter + 1
                target.FieldByName(typeSource.Field(i).Name).Set(source.Field(i))

            }
        }
    }

    return uint(counter)
}