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.

60 questions
1323
votes
11 answers

How to check if a map contains a key in Go?

I know I can iterate over a map m with for k, v := range m { ... } and look for a key, but is there a more efficient way of testing for a key's existence in a map?
grokus
  • 18,046
  • 9
  • 29
  • 35
16
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…
Trần Kim Dự
  • 5,872
  • 12
  • 55
  • 107
8
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…
3tbraden
  • 667
  • 1
  • 10
  • 21
6
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…
transient_loop
  • 5,984
  • 15
  • 58
  • 117
6
votes
2 answers

can golang function return interface{}{} - how to return a map list

func getLatestTxs() map[string]interface{}{} { fmt.Println("hello") resp, err :=…
眭文峰
  • 88
  • 1
  • 1
  • 7
5
votes
1 answer

Check if key exists in map storing large values

To know a key k exist in a map M1[k]v is very straightforward in Go. if v, ok := M1[k]; ok { // key exist } 'v': a value of a non-pointer type. If v is large, To just check if a particular key exists using the above method is not efficient as…
Prakash P
  • 3,582
  • 4
  • 38
  • 66
5
votes
3 answers

Check if a map is subset of another map

This question is already answered in many other languages. In golang with simple maps (no nesting) how to find out if a map is subset of another. for example: map[string]string{"a": "b", "e": "f"} is subset of map[string]string{"a": "b", "c": "d",…
Xaqron
  • 29,931
  • 42
  • 140
  • 205
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…
user892960
  • 309
  • 2
  • 11
3
votes
1 answer

add colly package output text to map in golang

i was making a web scraper with colly package, where it collects the ContestName and ContestTime from a website and make a json file. so i did like this Contests := make(map[string]map[string]map[string]map[string]string) …
3
votes
1 answer

Golang map with any key type and any value type

can i create in golang a map with any key type and any value type ? , something like : dict1 := map[interface]interface{} Thanks a lot !
Edgar
  • 43
  • 1
  • 4
2
votes
0 answers

Why doesn’t 'dive' work on gin? (about validator)

I just would like to do like the below about validation on Gin(Golang). type Accounts struct { Accounts []*Account `json:"accounts" binding:"required,dive"` } type Account struct { BusinessId string `json:"business_id"…
TomoEno
  • 21
  • 1
2
votes
1 answer

Rate limiter with gorilla mux

I am trying to implement http request limiter to allow 10 request per second per user by their usernames. At the max 10 request can be hit to the server including requests which are under processing. Below is what I have implemented with reference…
ganesh.go
  • 39
  • 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) { …
speller
  • 1,641
  • 2
  • 20
  • 27
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…
Aurélien Lambert
  • 712
  • 1
  • 8
  • 12
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,…
Ross Bown
  • 89
  • 1
  • 1
  • 11
1
2 3 4