Questions tagged [go-map]

Go provides a built-in map type that implements a hash table. It is an unordered group of elements of one type, called the element type, indexed by a set of unique keys of another type, called the key type. The value of an uninitialized map is nil. A nil map behaves like an empty map when reading, but attempts to write to a nil map will cause a runtime panic; don't do that.

38 questions
-1
votes
1 answer

How do I use RWMutex when concurrently modifying a map while iterating over it

I want to use a map's keys to request something from an API and then update the corresponding values based on the API's response. My guess would be the following code. Or maybe scratch this approach, collect the map-keys in an array before…
Rico
  • 21
  • 3
-1
votes
2 answers

When the form parameter in go is map, what is passed in?

When the formal parameter is map, assigning a value directly to a formal parameter cannot change the actual argument, but if you add a new key and value to the formal parameter, the actual argument outside the function can also be seen. Why is…
Wongfy
  • 27
  • 2
-1
votes
1 answer

Check if a value exists in map using an internal function from that struct

I've this struct type Zones map[uint64]Zone And I want to have a method to find a value in that map, something like this. func (z *Zones) findById(id uint64) (Zone, error) { if zone, ok := z[id]; ok { return zone, nil } else { …
Martin
  • 1,103
  • 10
  • 20
-2
votes
1 answer

Slice of strings vs map of string with dummy values for efficient deletion in golang

I will be receiving strings one by one from a framework, I need to hold them in some container and delete some of them later. Now I have 2 options :- Create a slice of strings and then delete some items by look up Create a map of string with…
Anand Shah
  • 83
  • 8
-2
votes
2 answers

How to return key's value of a map of type empty interface

I have taken a variable like var u = make(map[string]interface{}) which means that a key could hold a string/int or another map. When I do the following it gives error cannot use v (type interface {}) as type string in return argument: need type…
Volatil3
  • 11,721
  • 33
  • 112
  • 218
-2
votes
1 answer

Appending to a slice that is a value in a map

I want to append to a slice that is a value of a map, e.g. given m map[string][]string: if values, exists := m[key]; exists { values = append(values, v) // I don't want to call: m[key] = values } else { m[key] = []string{ v } } That…
isapir
  • 14,475
  • 6
  • 82
  • 97
-3
votes
1 answer

Map concurrent usage

I came across this piece of code and was wondering if this needs to have a R/W Mutex. method(){ var ( wg sync.WaitGroup rwm sync.RWMutex vcnRegionMap map[string][]core.Vcn ) vcnRegionMap =…
f-z-N
  • 1,377
  • 3
  • 21
  • 35
-3
votes
2 answers

How to append items to a map[string][]Struct

I am trying to append items to this struct I have: type AuditSource struct { Source map[string][]Pgm `json:"Source"` } type Pgm struct { ID uint `json:"id,omitempty"` SourceIP Inet `json:"sourceip,omitempty"` …
Jay266
  • 869
  • 1
  • 11
  • 22
1 2
3