当前位置: > > > > 在运行时比较和合并 2 个结构体
在运行时比较和合并 2 个结构体
来源:stackoverflow
2024-04-19 10:12:37
0浏览
收藏
来到米云的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《在运行时比较和合并 2 个结构体》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!
问题内容
我正在尝试在运行时比较两个结构。我似乎无法一一比较这个领域。 我想我需要在运行循环时为每个字段转换类型,但是 reflect.typeof() 不会给我“类型”的预期结果(在这种情况下为 int / string )。 我想这是因为我提供了一个接口{}作为参数?有什么办法让它发挥作用吗?
我的目标是能够比较相同类型的 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
}
解决方案
请注意,像问题中的简单结构可以与 == 进行比较。
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)
}
另请注意,字段的顺序是静态的,为了比较相同类型的两个结构的值,您可以执行基本循环并使用 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()
}
}
要合并两个不同结构的值,您可以从以下开始:
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)
}
}
}
终于介绍完啦!小伙伴们,这篇关于《在运行时比较和合并 2 个结构体》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~米云公众号也会发布Golang相关知识,快来关注吧!
