mardi 19 janvier 2021

Getting the base type of a custom type in Go using Reflect

Say I create a custom type in Go:

type CustomTime time.Time

Using reflection, I'm comparing types, e.g.

var foo CustomTime = CustomTime(time.Now())
customType := reflect.TypeOf(foo)

timeType := reflect.TypeOf(time.Now())

if customType == timeType {
    fmt.Println("Is timeType")
} else {
    fmt.Println("Is not timeType")
}

This prints "Is not timeType". What I'm trying to do is find a way to see if the base type that the custom type uses (i.e. the time.Time in type CustomType time.Time) is of type time.Time.

I've tried using reflect's Kind() function, but that returns struct for both since time.Time is a struct.

Here's a playground with this code.





Aucun commentaire:

Enregistrer un commentaire