gpt4 book ai didi

go - Reflection.DeepEqual()返回false但 slice 相同

转载 作者:行者123 更新时间:2023-12-03 10:09:46 27 4
gpt4 key购买 nike

我正在比较两个均为[]int类型的 slice 。一种以json的形式进入API并解析为go struct。在结构中,它被初始化为空[]int{}。第二个保存在数据库(MongoDb)中,并提取并映射到相同的结构类型。
在某些情况下,两个 slice 完全空白。但是比较返回falsereflect.DeepEqual(oldSettings.S1, newSettings.S1)我还使用reflect.TypeOf(newSettings.S1)检查了两个字段的类型。两者都在重新调整[]int
请考虑此游乐场链接以获取结构示例。
https://play.golang.org/p/1JTUCPImxwq

type OldSettings struct {
S1 []int
}

type NewSettings struct {
S1 []int
}

func main() {
oldSettings := OldSettings{}
newSettings := NewSettings{}

if reflect.DeepEqual(oldSettings.S1, newSettings.S1) == false {
fmt.Println("not equal")
} else {
fmt.Println("equal")
}
}
谢谢!

最佳答案

如果一个 slice 是reflect.DeepEqual(),而另一个 slice 是具有false长度的非nil slice ,则 nil 返回0。引用reflect.DeepEqual()的文档:

Slice values are deeply equal when all of the following are true: they are both nil or both non-nil, they have the same length, and either they point to the same initial entry of the same underlying array (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal. Note that a non-nil empty slice and a nil slice (for example, []byte{} and []byte(nil)) are not deeply equal.


例子:
oldSettings := OldSettings{S1: []int{}}
newSettings := NewSettings{}

if reflect.DeepEqual(oldSettings.S1, newSettings.S1) == false {
fmt.Println("not equal")
} else {
fmt.Println("equal")
}
输出(在 Go Playground上尝试):
not equal
如果JSON或MongoDB源代码中不存在 S1字段,则在将其编码后将保留为 nil。但是,如果它以空数组形式存在,则会在Go中为其创建一个空的非 nil slice 。
示例证明:
var s struct {
S1 []int
}

if err := json.Unmarshal([]byte(`{}`), &s); err != nil {
panic(err)
}
fmt.Println(s, s.S1 == nil)

if err := json.Unmarshal([]byte(`{"S1":[]}`), &s); err != nil {
panic(err)
}
fmt.Println(s, s.S1 == nil)
哪个输出(在 Go Playground上尝试):
{[]} true
{[]} false

关于go - Reflection.DeepEqual()返回false但 slice 相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64643402/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com