Re: [go-nuts] nil map assignment panics. can we do better?

2019-09-24 Thread Dan Kortschak
You can write that. func insert(m map[K]V, k K, v V) map[K]V { if m == nil { return map[K]V{k: v} } m[k] = v return m } On Tue, 2019-09-24 at 13:10 -0700, Marcin Romaszewicz wrote: > Could we have an operation like append() for slices? > > How

Re: [go-nuts] nil map assignment panics. can we do better?

2019-09-24 Thread Jan Mercl
On Tue, Sep 24, 2019 at 10:11 PM Marcin Romaszewicz wrote: > > Could we have an operation like append() for slices? > > How about: > > m := insert(m, "key", "value"). A slice is a value but a map is implemented as a pointer. IOW, the line of code quoted above updates only the locally visible

Re: [go-nuts] nil map assignment panics. can we do better?

2019-09-24 Thread Marcin Romaszewicz
Could we have an operation like append() for slices? How about: m := insert(m, "key", "value"). It returns m unchanged if it's allocated, otherwise, it allocates. We're already familiar with this kind of function. -- Marcin On Mon, Sep 23, 2019 at 10:58 PM abuchanan via golang-nuts <

Re: [go-nuts] nil map assignment panics. can we do better?

2019-09-23 Thread abuchanan via golang-nuts
Ah, thanks Ian, that's exactly the kind of requirement I was looking for. (also, you said "without" but you probably meant "while") Perhaps this is a job for "go vet". And/or, looks like staticcheck.io has a check I can try: https://staticcheck.io/docs/checks#SA5000 On Monday, September

Re: [go-nuts] nil map assignment panics. can we do better?

2019-09-23 Thread Ian Lance Taylor
On Mon, Sep 23, 2019 at 2:40 PM abuchanan via golang-nuts wrote: > > Is there anything written down on why assignment to a nil map causes a panic? > I'm curious if there were carefully considered tradeoffs while making this > decision. > > Assignment to nil maps has caused most of the

[go-nuts] nil map assignment panics. can we do better?

2019-09-23 Thread abuchanan via golang-nuts
Is there anything written down on why assignment to a nil map causes a panic? I'm curious if there were carefully considered tradeoffs while making this decision. Assignment to nil maps has caused most of the unexpected panics I've seen. Many times these happen in production and cause an