mardi 28 février 2017

golang DeepEqual and reflect.Zero

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 : http://ift.tt/2lklc2B

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

Thanks !





Aucun commentaire:

Enregistrer un commentaire