This is because per the Go spec:
https://golang.org/ref/spec#Type_declarations
You're not creating an alias to Token, but defining a new type from it. If
what you are looking for is an alias (meaning to have the "WrappedToken"
identifier refer to the exact same type as Token), you need to do:

type WrappedToken = Token

Then all method calls on WrappedToken will be method calls on Token (since
both identifier denote the same type).

But maybe that is not what you are looking for, if that's the case, there
are other ways around the problem, like type embedding and such.
Hope this helps,

Le lun. 15 mars 2021 à 13:49, [email protected] <[email protected]> a écrit :

> I've just tried doing something like this in my code:
>
> // Token is an OAuth2 token which includes decoding the expires_in
> attribute
> type Token struct {
> oauth2.Token
> ExpiresIn int `json:"expires_in"` // expiration time in seconds
> }
>
> func (t *Token) UnmarshalJSON(data []byte) error {
> ...
> }
>
> type WrappedToken Token
>
> Only to find out, that unmarshaling a WrappedToken does not call
> Token.UnmarshalJSON which came as a bit of surprise.
>
> I can only guess it's due to how JSON unmarshaling works, but is this the
> intended behaviour?
>
> Kind regards,
> Andreas
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/0ceb1917-af61-4259-8695-55eeb0bd6bffn%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/0ceb1917-af61-4259-8695-55eeb0bd6bffn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAL4P9zzd8YpC7QuFO6wBxxHxmeNGQtxYmATVscKs2m3_tfMvZw%40mail.gmail.com.

Reply via email to