Menu

When to Prefer `any` over Interfaces?

#golang#redditgolang#metadata#type#fields
Reading 0:00
15s threshold

Hello,

I'm unfamiliar on how I should solve this in go.

I receive what I am calling Metadata over http in my handler. I receive the body and decode / unmarshal it into a request DTO something like

type MetadataRequest struct { // generic metadata fields like createdAt etc. flavoredMetadata json.RawMessage }

As you can see, Metadata can be in various flavors from various sources. Ultimately it is all Metadata, just that metadata might be in a different shape and have different data depending on the source.

I want to unmarshal the flavoredMetadata into a type specific flavor like:

type RedMetadata struct { // red metadata specific fields + generic metadata fields } type BlueMetadata struct { // blue metadata specific fields + generic metadata fields }

In other languages, I would typically have BlueMetadata and RedMetadata implement an interface of a common type like Metadata, so maybe they both have a Validate method

type Metadata interface { Validate() error }

So I'd give both RedMetadata and…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.