If you embed multiple types that have identically named fields or methods, then those fields or methods will not be accessible directly anymore.
Selectors:
x.f
- For a value
x
of type T
or *T
where T
is not a pointer or
interface type, x.f
denotes the field or method at the shallowest depth
in T
where there is such an f
. If there is not exactly one f
with shallowest depth, the selector expression is illegal.
That means that, given the following set of types:
type S1 struct { F string }
type S2 struct { F string }
type S3 struct {
S1
S2
}
the expression s3.F
is illegal:
var s3 S3
_ = s3.F // ambiguous selector s3.F
this is because there are more than one F
at the shallowest depth. You can try it on playground.
The same rules apply to methods. From this it follows that your type C
does NOT satisfy the json.Marshaler
interface because it embeds, at the same depth, more that one type that implements the MarshalJSON()
method. You can see that for yourself on playground.
Then, however, the question still remains as to why the embedded time.Time
's custom marshaling is used regardless. This is because time.Time
implements not only the json.Marshaler
interface but also the encoding.TextMarshaler
interface (docs & playground). And the json.Marshal
's documentation says the following:
Marshal traverses the value v recursively. If an encountered value
implements the Marshaler
interface and is not a nil pointer, Marshal
calls its MarshalJSON
method to produce JSON. If no MarshalJSON
method
is present but the value implements encoding.TextMarshaler
instead,
Marshal calls its MarshalText
method and encodes the result as a JSON
string.
You can see here that the behaviour described above holds once you also have A
or B
implement the encoding.TextMarshaler
interface, then the time.Time
's MarshalText
method will not be used anymore.