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 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 <
> golang-nuts@googlegroups.com> wrote:
> 
> > 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 23, 2019 at 9:45:37 PM UTC-7, Ian Lance Taylor
> > wrote:
> > > 
> > > 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 unexpected panics
> > > > I've
> > > 
> > > seen. Many times these happen in production and cause an
> > > incident. Every
> > > time it happens I think, "we've got to do something about this."
> > > > 
> > > > I'm not sure what the best solution is yet, but there must be
> > > > something
> > > 
> > > better than the status quo. Would it be possible to have
> > > assignments
> > > automatically initialize a nil map?
> > > 
> > > I agree that this is awkward.  But we've never been able to think
> > > of a
> > > way to make an assignment to a nil map initialize the map without
> > > also
> > > preserving the very useful property that the zero value of any Go
> > > type
> > > can be represented by a sequence of zero bytes.
> > > 
> > > Ian
> > > 
> > 
> > --
> > You received this message because you are subscribed to the Google
> > Groups
> > "golang-nuts" group.
> > To unsubscribe from this group and stop receiving emails from it,
> > send an
> > email to golang-nuts+unsubscr...@googlegroups.com.
> > To view this discussion on the web visit
> > 
https://groups.google.com/d/msgid/golang-nuts/d9cc8fb3-7d73-48c0-8c60-9d9b1022054c%40googlegroups.com
> > <
> > https://groups.google.com/d/msgid/golang-nuts/d9cc8fb3-7d73-48c0-8c60-9d9b1022054c%40googlegroups.com?utm_medium=email_source=footer
> > >
> > .
> > 
> 
> 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ef644d6aa635812a5d17cbcba3b763ef74598984.camel%40kortschak.io.


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 copy of m.
All other copies of m are left at the old, possibly nil, value, but
that's not how maps in Go work. Once the map is initialized, all the
shared copies can access the same values/operations on it.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAA40n-X06rRJh9%3Dehkv7OszkNx2Lg2UyLzFqHesS4HF5d%3DU7fA%40mail.gmail.com.


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 <
golang-nuts@googlegroups.com> wrote:

> 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 23, 2019 at 9:45:37 PM UTC-7, Ian Lance Taylor wrote:
>>
>> 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 unexpected panics I've
>> seen. Many times these happen in production and cause an incident. Every
>> time it happens I think, "we've got to do something about this."
>> >
>> > I'm not sure what the best solution is yet, but there must be something
>> better than the status quo. Would it be possible to have assignments
>> automatically initialize a nil map?
>>
>> I agree that this is awkward.  But we've never been able to think of a
>> way to make an assignment to a nil map initialize the map without also
>> preserving the very useful property that the zero value of any Go type
>> can be represented by a sequence of zero bytes.
>>
>> Ian
>>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/d9cc8fb3-7d73-48c0-8c60-9d9b1022054c%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CA%2Bv29LvMsYoVs4Q6TiUdNfb4eJjWcAcD5t4xaxuxLxwqpvo9Dg%40mail.gmail.com.


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 23, 2019 at 9:45:37 PM UTC-7, Ian Lance Taylor wrote:
>
> 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 unexpected panics I've 
> seen. Many times these happen in production and cause an incident. Every 
> time it happens I think, "we've got to do something about this." 
> > 
> > I'm not sure what the best solution is yet, but there must be something 
> better than the status quo. Would it be possible to have assignments 
> automatically initialize a nil map? 
>
> I agree that this is awkward.  But we've never been able to think of a 
> way to make an assignment to a nil map initialize the map without also 
> preserving the very useful property that the zero value of any Go type 
> can be represented by a sequence of zero bytes. 
>
> Ian 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/d9cc8fb3-7d73-48c0-8c60-9d9b1022054c%40googlegroups.com.


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 unexpected panics I've seen. 
> Many times these happen in production and cause an incident. Every time it 
> happens I think, "we've got to do something about this."
>
> I'm not sure what the best solution is yet, but there must be something 
> better than the status quo. Would it be possible to have assignments 
> automatically initialize a nil map?

I agree that this is awkward.  But we've never been able to think of a
way to make an assignment to a nil map initialize the map without also
preserving the very useful property that the zero value of any Go type
can be represented by a sequence of zero bytes.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcV2XgwS-C-FKvBfSNp3unYdEfk0K2OZZywCZUTAtU-%3D0w%40mail.gmail.com.