Open
Description
In-short:
m[key].Method()
where you don't control map keys fully, and can't be certain that the value exists, will make it easy to panic or operate on invalid values. e..g if map[T]Foo
with a value receiver, m[key].Method()
will operate on the zero value of Foo
, which may not be desirable. And given map[T]*Foo
, m[key].Method()
will operate on nil, which Method()
likely doesn't handle unless explicitly specified and planned.
I don't think we should forbid it outright because when you do explicitly have the zero-value-behavior (nil if pointer) in mind, m[key].Method()
is more readable than:
if v, ok := m[key]; ok {
v.Method()
}
CC @peterbourgon who brought this up in #170.
Metadata
Assignees
Labels
No labels
Activity