Delete from map failed golang

96 views
Skip to first unread message

Van Fury

unread,
Nov 19, 2021, 10:17:43 AM11/19/21
to golang-nuts
Hi

I have a map as shown below

var CtxStarStore = make(map[string]AuthCtx)

map[ccc-999401002:{989c48c5-7f9c-4058-80a2-b769ea4675f9 imei-1234010002} bbb-999401003:{989c48c5-7f9c-4058-80a2-b769ea4675f9 imei-1234010003}]

I would like to delete ccc-999401002  or any key value from the map I tried delete as
s := "ccc-999401002"

delete(CtxStarStore, s)

Any reason why I cannot delete it

Levieux Michel

unread,
Nov 19, 2021, 10:29:53 AM11/19/21
to Van Fury, golang-nuts
Hi,

I reported your code into the playground for testing, and it seems to be working fine for me: https://play.golang.com/p/udsRN-iUQM8
I designed a simple AuthCtx struct based on your example but that should not change anything.

Maybe it would be easier to help with more details. Why do you say you *can't* delete? What happens when you try to? What problem are you trying to solve?

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5a863263-94cd-463e-b932-2d788985a108n%40googlegroups.com.

Kurtis Rader

unread,
Nov 19, 2021, 11:09:04 AM11/19/21
to Van Fury, golang-nuts
On Fri, Nov 19, 2021 at 7:17 AM Van Fury <fury...@gmail.com> wrote:
I have a map as shown below

var CtxStarStore = make(map[string]AuthCtx)

map[ccc-999401002:{989c48c5-7f9c-4058-80a2-b769ea4675f9 imei-1234010002} bbb-999401003:{989c48c5-7f9c-4058-80a2-b769ea4675f9 imei-1234010003}]

This isn't legal Go.
 
I would like to delete ccc-999401002  or any key value from the map I tried delete as
s := "ccc-999401002"

delete(CtxStarStore, s)

That should work once you correct the initialization of the map.
 
Any reason why I cannot delete it

No idea since you didn't provide us with an example of code that compiles. It's usually a good idea to enter your example at https://play.golang.com to verify it at least compiles then include a link to that code in your message.

 --
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Van Fury

unread,
Nov 19, 2021, 11:57:33 AM11/19/21
to golang-nuts
Hi,

Sorry, it was my stupid mistake. I was deleting with wrong key.

@Kurtis: "That should work once you correct the initialization of the map"
                 Is my map initialization not the best way?
                 I mean
                var CtxStarStore = make(map[string]AuthCtx)

               I would like to CtxStarStore would be access globally.
               Is there a better way to initialize it.
             
Thanks for the response.

--
Fury

Brian Candler

unread,
Nov 19, 2021, 12:27:55 PM11/19/21
to golang-nuts
>  I would like to CtxStarStore would be access globally.

As a general principle, I'd say try to avoid such global state.  Most data structures in go are not safe for concurrent access from multiple goroutines: not only maps, but even things like integers.  Concurrent access to maps or slices can cause your program to panic.

Obviously there's the traditional solution of "use a mutex around all operations".  There are also some safe structures like sync.Map.  But it may be that there's a better pattern for your application.

If you can invest the time in this video from Bryan C Mills - and it took me several views to get it fully - it's very much worth it:

One thing I learned from this is that you can keep data in a 1-element buffered channel, popping it out when you need to access it, and pushing it back in when you're done.
Reply all
Reply to author
Forward
0 new messages