Skip to content

[question] Why can't Encoder check and call fmt.Stringer.String() on element automatically? #196

Open

Description

@howellzhu

Why can't Encoder directly determine whether an element implements an interface (such as fmt.Stringer or encoding.TextMarshaler) and actively call its interface to implement Encode()?

After all, Decode already does that (using encoding.TextUnmrshaler interface).

Activity

  1. jaitaiwan commented on Aug 20, 2024

    @jaitaiwan

    I can look at this again, but from my understanding, go doesn't have a specific way to check if a type implements an interface. We'd have to do reflection and see if a method with a particular name existed. I'm not sure what the benefit of doing this would actually be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions