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.
Questions tagged [go-map]
60 questions
-1
votes
1 answer
Values in map changing after refresh
Im trying to create a map that remembers its values when the page is recalled. I declared it outside the function so it would remain the same but the map still initializes to the defauly=t values when the page is recalled. How can i get it to…

jcezetah
- 3
- 1
-1
votes
1 answer
How to ensure that a Struct that is made up of channels and map gets passed by reference?
I have the following struct that contains channels and a map for storage of data. I want to be able to pass that struct into functions in order to make use of those channels so that once they are triggered/have incoming messages, to use them in…

AC007
- 137
- 9
-1
votes
1 answer
Golang - Generate map with one to many relation
I am fairly new to golang and I am struggling to generate a one to many relationship map from existing map.
Here is my script playground
Explanation:-
I am trying to achieve the relation of each element of 0th position to each element of…

Bhargavi Pise
- 7
- 2
-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
- 4
-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
- 47
- 3
-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,141
- 10
- 23
-1
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
- 21,295
- 13
- 115
- 116
-2
votes
1 answer
Best practice of counter in map struct golang
I have a code that is not clean. I want to increment Counter in struct of NameLike but I think this is not effective.
package main
import "fmt"
type NameLike struct {
Name string
Counter int
}
func main() {
sosmed :=…

Rizal Arfiyan
- 181
- 1
- 9
-2
votes
3 answers
Golang: Appending keys from a map to a slice of slices
I ran into this simple Golang code and was surprised by Go's behavior here. Can someone explain what is going on here, and how to write the below code correctly?
As you can see, I have a map, where the key is an array of int. I add a couple of…

lajosdeme
- 2,189
- 1
- 11
- 20
-2
votes
1 answer
Is deletion from slice of strings is faster than map of string with dummy values considering number of elements will be less than 500
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
- 133
- 10
-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
- 14,253
- 38
- 134
- 263
-3
votes
1 answer
How to acheive the order of insetion in nested maps using golang
In my project we are using nested map
map[string]map[string]map[string]string
for insertion of the data into cache.
While preparing the data the db data has to be compared with the cache data(nested map data).
Here is the concern: we need order of…

madhusri mannuru
- 41
- 5
-3
votes
1 answer
Unable to parse Golang map and nested json
I have below snippet
func fakeGetInclusterConfig() (*corev1.ConfigMap, error) {
configMap := &corev1.ConfigMap{
Data: map[string]map[string]string{"cluster-config.json":{
"cluster_id":"xxx",
…

ambikanair
- 4,004
- 11
- 43
- 83
-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,645
- 4
- 24
- 38
-3
votes
1 answer
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"`
…

lakeIn231
- 1,177
- 3
- 14
- 34