将字段/值从src复制到dest对象

I am trying to copy the fields from one struct value to another, where they have the same field definitions. I have this program:

package main

import (

    "log"
    "reflect"
)


func setExistingFields(src interface{}, dst interface{}) {

    fields := reflect.TypeOf(src)
    values := reflect.ValueOf(src)

    num := fields.NumField()
    s := reflect.ValueOf(src).Elem()
    d := reflect.ValueOf(dst).Elem()

    for i := 0; i < num; i++ {
        field := fields.Field(i)
        value := values.Field(i)
        fsrc := s.FieldByName(field.Name)
        fdest := d.FieldByName(field.Name)

        if fdest.IsValid() && fsrc.IsValid() {

            if fdest.CanSet() && fsrc.CanSet() {

                fdest.Set(value)

            }
        }

    }
}

// and then we main:
func main() {

    src := struct {
        Foo string
        Bar string
    }{
        "dog",
        "pony",
    }

    dest := struct{ Foo string; Bar string }{}
    setExistingFields(&src, &dest)

    log.Println("dest.Foo", dest.Foo)
}

I run that, but I get an error:

reflect: NumField of non-struct type

I can't figure out what that's about.

Here's a playground link: https://play.golang.org/p/TsHTfAaeKhc

Try this out:

func setExistingFields(src interface{}, dst interface{}) {

    srcFields := reflect.TypeOf(src).Elem()
    srcValues := reflect.ValueOf(src).Elem()

    dstValues := reflect.ValueOf(dst).Elem()

    for i := 0; i < srcFields.NumField(); i++ {
        srcField := srcFields.Field(i)
        srcValue := srcValues.Field(i)

        dstValue := dstValues.FieldByName(srcField.Name)

        if dstValue.IsValid() {
            if dstValue.CanSet() {
                dstValue.Set(srcValue)
            }
        }

    }
}

Note that you need to do additional checking if src field value is assignable to dst field type.

Edit: The reason why you are getting that error is because fields at that point is a pointer to a struct. You need to get the actual struct value by using Elem().

This won't work: A struct always gets its "schema" (eg. its fields) during compile time... You cannot add more fields during runtime.

I don't see what your exact use case is, but consider something like map[string]string or even map[string]interface{} to be able to "extend" the content/fields of the thing you are passing around...