golang的DeepEqual和reflect.Zero

huangapple go评论84阅读模式
英文:

golang DeepEqual and reflect.Zero

问题

我正在尝试使用reflect.DeepEqual来检查结构体中的字段是否设置为其零值。我的想法是,如果是这种情况,我可以使用作为结构体标签给出的默认值来更改其值,如下所示:

type MyStruct struct {
    A int `default:"42"`
}

我的问题是:看起来reflect.DeepEqual总是返回false。我觉得我漏掉了什么。以下是我尝试做的简单示例:

package main

import (
    "fmt"
    "reflect"
)

func main() {
    s := MyStruct{A: 0}
    field := reflect.ValueOf(s).Field(0)

    fmt.Println(field.Interface())
    fmt.Println(reflect.Zero(field.Type()))

    fmt.Println(reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type())))
}

这是上述代码的Go Playground版本:https://play.golang.org/p/k9VY-2Dc69

我想知道为什么在这种情况下DeepEqual返回false

谢谢!

英文:

I'm trying to check if a field in a struct is set to its zero value with reflect.DeepEqual. The idea is that if it is the case, I can change its value with a default one given as a struct tag like following :

type struct {
    A int `default:"42"`
}

My problem is the following : It looks like reflect.DeepEqual is always returning me false. I think I'm missing something. Here is a simple example of what I'm trying to do:

package main

import (
    "fmt"
    "reflect"
)

func main() {
    s := struct{ A int }{0}
    field := reflect.ValueOf(s).Field(0)

	fmt.Println(field.Interface())
    fmt.Println(reflect.Zero(field.Type()))

	fmt.Println(reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type())))
}

And here is a go playground version of the code above : https://play.golang.org/p/k9VY-2Dc69

I would like to know why DeepEqual is returning false in this case.

Thanks!

答案1

得分: 3

在这里:

reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type()))

你正在比较一个被包装在 interface{} 中的 int 值,与一个类型为 reflect.Value 的值。reflect.Zero() 返回的是一个 reflect.Value 类型的值,而不是 interface{}

你忘记在第二个参数上调用 Value.Interface()

reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type()).Interface())

这将打印出 true。你可以在 Go Playground 上尝试一下。

英文:

Here:

reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type()))

You are comparing an int value (wrapped in interface{}), to a value of type reflect.Value. reflect.Zero() returns a value of type reflect.Value, not interface{}.

You forgot to call Value.Interface() on the 2nd argument:

reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type()).Interface())

This prints true. Try it on the Go Playground.

答案2

得分: 1

问题与您对reflect包的使用有关。

field.Interface()

返回一个int(包装在interface{}中),而

reflect.Zero(field.Type())

返回一个reflect.Value类型的对象。fmt.Println以相同的方式打印它们,但这并不意味着它们是相同的类型。

请参考此playground链接:https://play.golang.org/p/mLcLSV_0vA

英文:

The issue has to do with your use of the reflect package.

field.Interface()

Returns an int (wrapped in an interface{}), while

reflect.Zero(field.Type())

returns an object of type reflect.Value. fmt.Println prints them both out the same way, but that doesn't mean they are the same type.

See this playground: https://play.golang.org/p/mLcLSV_0vA

huangapple
  • 本文由 发表于 2017年2月28日 22:54:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/42511940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定