top | item 41924187

(no title)

n0w | 1 year ago

The API surface of some struct is determined by visibility, not by whether a member of the struct is a method or a field.

I can't remember the specifics for why fields cannot be used within a Go interface but I do remember missing it a few times while writing Go code.

discuss

order

fwip|1 year ago

I think the reasoning is that interfaces are implemented by a dynamic lookup. Part of Go's philosophy is that things that could be expensive (function calls) should be visually distinct from cheap things.

Struct field access is cheap, hopping through a dynamic dispatch table is less cheap.

rtpg|1 year ago

Took me a second to grok why the field access would require dynamic dispatch and it's because you have to deal with differing layout between structs.