go - 在运行时比较和合并 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
}
解决方案
请注意,可以将问题中的简单结构与==
.
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)
}
}
}
推荐阅读
- angular - 显示来自二进制流的图像 - Angular 10
- nginx - Upstream解析为IPv6时Nginx连接失败
- mysql - 通过使用 MySQL 触发器检查 AFTER Update 语句中的 OLD 值来更新新值
- java - 如何将 http:namenode-ip:50070/conf XML 响应转换为 JSON 类型?
- c# - WCF:发送带有自定义标头的 SOAP 请求
- c++ - Libreoffice API (UNO):需要更改用户的 xTextField 文本
- ruby - 新手问题 - 代码在 irb 中有效,但不是来自 ruby 脚本
- sql-server - 如何从 SQL 中的字符串中删除一组单词?
- sql - 将值插入以逗号分隔的多行
- flutter - 使用 AnimatedSwitcher 和 Dismissible 的页面滑动行为