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]
38 questions
958
votes
11 answers
How to check if a map contains a key in Go?
I know I can iterate over a map m by,
for k, v := range m { ... }
and look for a key but is there a more efficient way of testing a key's existence in a map?
I couldn't find the answer in the language spec.
![](../../users/profiles/203091.webp)
grokus
- 13,644
- 8
- 27
- 33
8
votes
2 answers
Difference between map[string]interface{} and interface{}
I want to parse a JSON file to a map[string]interface{}:
var migrations map[string]interface{}
json.Unmarshal(raw, &migrations)
fmt.Println(migrations["create_user"])
But I modified my code to point data to interface{}:
var migrations…
![](../../users/profiles/1493868.webp)
Trần Kim Dự
- 4,962
- 6
- 39
- 81
5
votes
1 answer
What is the default value of a map of struct
What is the default value of struct in a map? How to check the map value is initialized?
type someStruct struct {
field1 int
field2 string
}
var mapping map[int]someStruct
func main() {
mapping := make(map[int]someStruct)
}
func…
![](../../users/profiles/7827687.webp)
tbraden
- 397
- 3
- 14
5
votes
2 answers
can golang function return interface{}{} - how to return a map list
func getLatestTxs() map[string]interface{}{} {
fmt.Println("hello")
resp, err :=…
![](../../users/profiles/9662545.webp)
眭文峰
- 78
- 1
- 7
4
votes
2 answers
struct type as map key
We have a following function:
func (h *Handler) Handle(message interface{}) error {
//here there is a switch for different messages
switch m := message.(type) {
}
}
This signature is given and can't be changed. There are around 20…
![](../../users/profiles/169252.webp)
transient_loop
- 5,144
- 12
- 46
- 95
4
votes
1 answer
How to parse a JSON into a map with variable type in Golang
I have the following JSON response from the Salt-Stack API:
{
"return": [{
"": true,
"": "Minion did not return. [No response]",
"": true,
"": false
}]
}
I usually use a map…
![](../../users/profiles/892960.webp)
user892960
- 127
- 8
2
votes
1 answer
How to use GoMap in Cgo?
I'm trying to call Go from c++. My code operates on maps, and I can't seem to make maps work with cgo.
main.go:
package main
import (
"C"
"fmt"
)
func main() {}
//export PrintMap
func PrintMap(m map[string]string) {
…
![](../../users/profiles/804813.webp)
speller
- 1,290
- 2
- 13
- 24
2
votes
1 answer
How to know if 2 go maps reference the same data
Go maps are references to internal data. Meaning that when a map is "copied", they end up sharing the same reference and thus editing the same data. This is something highly different than having another map with the same items. However, I cannot…
![](../../users/profiles/2427501.webp)
Aurélien Lambert
- 572
- 1
- 5
- 11
2
votes
2 answers
Deleting multiple values from a map in Go at the same time within a loop
I'm trying to delete multiple values from my map[string][]interface{}
I am using the strings.Split function to separate each value i wish to delete, and then looping through them.
I have managed to get it so i can delete index values 0 and 1,…
![](../../users/profiles/8876584.webp)
Ross Bown
- 79
- 1
- 9
1
vote
1 answer
Encapsulating "concurrency safety" in Go Maps
I have a struct, MyStruct, which contains a map. I want to make the access to the map safe for concurrent read and write but I also want to stick to the base Map and not use sync.Map.
For this reason I create on MyStruct methods for insert, delete…
![](../../users/profiles/5699993.webp)
Picci
- 12,727
- 9
- 48
- 85
1
vote
0 answers
Are there any programming pitfalls of using a map with an empty interface as the KEY
Are there any programming pitfalls of using maps in this manner:
type Set struct {
theMap map[interface{}]struct{}
}
StringSet := NewSet("abc", "pqr")
IntSet := NewSet(1, 2)
DateSet := NewSet(time.Date(2021, 2, 15, 0, 0, 0, 0, time.UTC))
Just…
![](../../users/profiles/15558657.webp)
Sangam
- 51
- 5
1
vote
1 answer
Using msgp with interfaces and maps in Go
I have a map that uses an interface as the key. The map is defined like this MyMap map[Signature]Packets. The interface is Signature, and there will be two structs A and B that implement this interface. I am also using msgp to serialize these two…
![](../../users/profiles/15611364.webp)
Sebastian
- 11
- 2
1
vote
1 answer
What is the best way to create an array and loop in template file
I am using Gin gonic for my Go project, and in my footer.tmpl, I will have more than 10++ navigation links, rather than write 'link' multiple times, it would be much easier if I create an array containing the links, and title and loop through it,…
![](../../users/profiles/4608714.webp)
Charas
- 1,509
- 4
- 16
- 42
1
vote
1 answer
How to pass a map as value?
I've a few Actors in my Golang App which require two maps to do their work. Those maps are generated by some intensive database transactions so I don't want to do that in every actor, as a result I've separated the maps generation from the Actors.…
![](../../users/profiles/5584140.webp)
Martin
- 1,103
- 10
- 20
1
vote
4 answers
Slice out of bounds using [][]int but works with map[int][]int
Why does this code work
graph := make(map[int][]int, 0)
graph[0] = append(graph[0], 1)
But if you replace first line with graph := make([][]int, 0) I get panic: runtime error: index out of range? It's very weird.
![](../../users/profiles/942236.webp)
maxflow
- 649
- 2
- 9
- 15