首页 > 解决方案 > 在运行时比较和合并 2 结构

问题描述

我试图在运行时比较 2 结构。我似乎无法一一比较这个领域。我在想我需要在运行循环时为每个字段转换类型,但reflect.TypeOf()没有给我预期的“类型”结果(在这种情况下是 int / string)。我在想这是因为我提供了一个 interface{} 作为参数?有什么办法让它工作吗?

我的目标是能够比较来自相同类型的 2 个结构的值,并在有任何差异时将这些值“合并”到一个结构中。

package main

import (
    "fmt"
    "reflect"
)

type A struct {
    Foo string
    Bar int
    Zoo int
}

func main() {
    a := &A{Foo: "qwer",Bar:1}
    b := &A{Foo: "zxcv",Bar:1}
    testRefactor(a,b)

}

func testRefactor(t *A,comp *A) {
    valt := reflect.ValueOf(t).Elem()
    //valComp := reflect.ValueOf(comp).Elem()
    for i:=0; i<valt.NumField();i++{
        //fieldStructComp := valComp.Type().Field(i).Name
        fieldStructT := valt.Type().Field(i).Name


    valueStructComp := getFieldValueByname(comp,fieldStructT)
    valueStructT := getFieldValueByname(t,fieldStructT)

    typex := reflect.TypeOf(valueStructT)
    fmt.Println(typex.String())

        fmt.Println(valueStructT)
        fmt.Println(valueStructComp)
        fmt.Println(valueStructT == valueStructComp)

    }
}

func getFieldValueByname(structName interface{},fieldname string) interface{} {
    r := reflect.ValueOf(structName)
        f := reflect.Indirect(r).FieldByName(fieldname)
       return f
}

标签: goreflection

解决方案


请注意,可以将问题中的简单结构与==.

func main() {
    a := &A{Foo: "qwer", Bar: 1}
    b := &A{Foo: "zxcv", Bar: 1}
    c := &A{Foo: "qwer", Bar: 1}
    d := &A{Foo: "zxcv", Bar: 1}

    fmt.Println(*a == *b)
    fmt.Println(*a == *c)
    fmt.Println(*b == *d)
}

https://play.golang.org/p/7W8qk6db4Uu


另请注意,字段的顺序是静态的,为了比较相同类型的两个结构的值,您可以执行基本循环并使用它i来访问两个结构实例的相应字段(类型和值)。

即该getFieldValueByname功能是不必要的,你不需要它。

func testRefactor(a, b *A) {
    av := reflect.ValueOf(a).Elem()
    bv := reflect.ValueOf(b).Elem()
    at := av.Type()
    bt := bv.Type()

    for i := 0; i < av.NumField(); i++ {
        afv := av.Field(i)
        bfv := bv.Field(i)

        aft := at.Field(i)
        bft := bt.Field(i)

        fmt.Printf("a.%s <%s> = %v\n", aft.Name, aft.Type, afv)
        fmt.Printf("b.%s <%s> = %v\n", bft.Name, bft.Type, bfv)
        fmt.Printf("== ? %t\n", afv.Interface() == bfv.Interface())
        fmt.Println()

    }
}

https://play.golang.org/p/G1EhMeYYqud


要合并两个不同结构的值,您可以从以下开始:

func testRefactor(a, b interface{}) {
    av := reflect.ValueOf(a).Elem()
    bv := reflect.ValueOf(b).Elem()
    at := av.Type()
    bt := bv.Type()

    for i := 0; i < av.NumField(); i++ {
        afv := av.Field(i)
        aft := at.Field(i)

        bfv := bv.FieldByName(aft.Name)
        bft, ok := bt.FieldByName(aft.Name)
        if !ok || aft.Type != bft.Type {
            continue
        }

        fmt.Printf("a.%s <%s> = %v\n", aft.Name, aft.Type, afv)
        fmt.Printf("b.%s <%s> = %v\n", bft.Name, bft.Type, bfv)
        fmt.Printf("== ? %t\n", afv.Interface() == bfv.Interface())
        fmt.Println()

        if afv.Interface() != bfv.Interface() {
            afv.Set(bfv)
        }

    }
}

https://play.golang.org/p/9alFY4oBNZh


推荐阅读