removing elements from a map with values of struct type

2,090 views
Skip to first unread message

Alexandru Moșoi

unread,
Aug 27, 2011, 10:00:28 AM8/27/11
to golang-nuts
I need to remove an entry from a map which values are of type struct
like in the following sample.


type foo struct { bar
int }

func main() {
spam := make(map[int]foo)
var egg foo
spam[0] = egg, false
}


Unless there is another way to remove an entry, I find the removal
operation very contrived because you need to create a value in order
to remove an entry.

Since egg is unnecessary, and '_' is usually used in places where the
literal is irrelevant I suggest the following way to remove from a
map:

spam[0] = _, false

or even simpler:

spam[0] = _

Bjorn Tipling

unread,
Aug 27, 2011, 4:38:54 PM8/27/11
to Alexandru Moșoi, golang-nuts
I like your idea. Also If you use a pointer you can do this:

package main

type foo struct { 
 bar int 
}

func main() {
       spam := make(map[int]*foo)
       spam[0] = nil, false
}

But maybe you don't want to use a pointer.

Miki Tebeka

unread,
Aug 27, 2011, 6:28:40 PM8/27/11
to golan...@googlegroups.com
Keeping values inside a map is *probably* not what you want to do. Since go will copy the object.
The following program will print 1, 2 (and not 2,2 as expected).
    package main

    import "fmt"


    type foo struct {
        bar int
    }

    func main() {
        spam := make(map[int]foo)
        f1 := foo{1}
        spam[0] = f1
        f2 := spam[0]
        f2.bar = 2

        fmt.Println(f1.bar)
        fmt.Println(f2.bar)
    }

You probably want to store pointers to your objects (like Bjorn suggested) and then delete using nil.

j...@webmaster.ms

unread,
Aug 28, 2011, 12:22:54 AM8/28/11
to golang-nuts
On Aug 28, 2:28 am, Miki Tebeka <miki.teb...@gmail.com> wrote:
> Keeping values inside a map is *probably* not what you want to do. Since go
> will copy the object.

There can be a struct of size comparable to pointer or less.
Something like image.RGBAColor or even image.GrayColor

It is ugly to construct new struct in order to remove an element from
the map

Nigel Tao

unread,
Aug 28, 2011, 3:58:26 AM8/28/11
to Alexandru Moșoi, golang-nuts
On 28 August 2011 00:00, Alexandru Moșoi <brt...@gmail.com> wrote:
> func main() {
>        spam := make(map[int]foo)
>        var egg foo
>        spam[0] = egg, false
> }

You can write map deletion as a one-liner:
spam[0] = foo{}, false


> Since egg is unnecessary, and '_' is usually used in places where the
> literal is irrelevant I suggest the following way to remove from a
> map:
>
> spam[0] = _, false
>
> or even simpler:
>
> spam[0] = _

This has been discussed before:
http://groups.google.com/group/golang-nuts/browse_thread/thread/35afce44285f2749
http://groups.google.com/group/golang-nuts/browse_thread/thread/55758d25014282db
http://groups.google.com/group/golang-nuts/browse_thread/thread/72f46ebfb9efebbf
http://code.google.com/p/go/issues/detail?id=561

The syntax could be nicer, but I don't think that the current syntax
is overly problematic.

John Asmuth

unread,
Aug 28, 2011, 9:14:07 AM8/28/11
to golan...@googlegroups.com
If your key is the struct type, then something of that size is getting copied around anyway - putting an extra Type{}, _ isn't going to affect performance.

And "ugly" is completely subjective. It looks fine to me.
Reply all
Reply to author
Forward
0 new messages