5

The section Interface checks from Effective Go recommends

var _ json.Marshaler = (*RawMessage)(nil)

as a compile-time check that RawMessage implements Marshaler.

I get how the assignment does the type check but what does the right side actually mean?

AndreKR
  • 32,613
  • 18
  • 106
  • 168
  • Does this answer your question? [Ensure a type implements an interface at compile time in Go](https://stackoverflow.com/a/60663003) –  Mar 13 '20 at 03:47

1 Answers1

7

Ok, I figured it out. It creates a new *RawMessage (pointer to RawMessage) that is nil by casting nil to *RawMessage. I would have expected

*RawMessage(nil)

but that doesn't work because the cast conversion seems to take precedence over the pointer operator, so it would become a dereference.

AndreKR
  • 32,613
  • 18
  • 106
  • 168