Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `(*RawMessage)(nil)` mean?

Tags:

go

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?

like image 650
AndreKR Avatar asked Mar 27 '16 07:03

AndreKR


1 Answers

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.

like image 120
AndreKR Avatar answered Sep 28 '22 22:09

AndreKR