Linked Questions
17 questions linked to/from How to check if a map contains a key in Go?
0 votes
1 answer
17k views
go - checking if key of one map present in another [duplicate]
I have one dictionary a = {1:11, 2:22} I want to check if key in b is present in a or not b = {3:33, 1:11} How can I do this in Go language? I have done it like this: a:= make(map[string][]string) ...
3 votes
1 answer
3k views
How to differentiate empty string and nothing in a map [duplicate]
The following code yields true. So I'm wondering for map[string]string in Golang, is there a way to differentiate empty string and nothing? package main import "fmt" func main() { m := make(map[...
0 votes
1 answer
1k views
How to tell if the value of a map is undefined in Go? [duplicate]
Suppose I have a map like: m := map[string]interface{}{} Now I get a string "a", I want to know if there's value in m["a"], how can I tell? As I can see now, m["a"] is never nil, so I can't compare ...
0 votes
0 answers
521 views
Checking if key exist in map which return interface type in go [duplicate]
I have a interface and one method. type IAlertType interface { CreateAlertDetail(alertId uint, attributes map[string]string,id uint) IAlertType } And a map. var ProductAlertMap = map[string]...
132 votes
9 answers
90k views
What is "_," (underscore comma) in a Go declaration?
And I can't seem to understand this kind of variable declaration: _, prs := m["example"] What exactly is "_," doing and why have they declared a variable like this instead of prs := m["example"] (I ...
108 votes
1 answer
59k views
How to check if a slice has a given index in Go?
We can easily do that with maps: item, ok := myMap["index"] But not with slices: item, ok := mySlice[3] // panic! Surprised this wasn't asked before. Maybe I'm on the wrong mental model with Go ...
53 votes
3 answers
39k views
What is the Big O performance of maps in golang?
The "Map types" section of the go language specification describes the interface and general usage of map types and the "Go maps in action" post on The Go Blog casually mentions ...
16 votes
3 answers
40k views
How can I create an array that contains unique strings?
I want to create an array that contains unique strings. How can I do that? var paths = make([]string, 0) func main() { // Members are added dynamically paths = append(paths, "aaa") paths ...
5 votes
3 answers
20k views
Check if a value exists in a map in Golang
I am searching for an efficient way to check if a value exists in Golang. The way I know is using a for loop/ range. Is there a better way to do the same? Any help would be appreciated :)
11 votes
2 answers
8k views
Check if key exists in multiple maps in one condition
I need to check if the same key exists in two maps: if v1, ok1 := map1["aaa"]; ok1 { ... } if v2, ok2 := map2["aaa"]; ok2 { ... } Is it possible to join these two conditions into one? I managed to ...
2 votes
2 answers
3k views
What is benefit of not raising error (like Python) when key is not in map? [closed]
In Go, let m is map object that map string to int, suppose "foo" is not a key of m, then the statement m["foo"] returns two value 0 and false which false implies "foo" is not a key of m. Could you ...
3 votes
2 answers
3k views
Determining if delete actually removed an existing key in a map
I have a map called nearby func Delete(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) delete(nearby, params["id"]) } I want to find out if the delete() call actually found a ...
4 votes
2 answers
4k views
What value does the return value take if something is not in a map?
Ok so according to this: How to check if a map contains a key in go? if val, ok := m["foo"]; ok { //do something here } that's fine, but how come we can't do this: val, ok := m["foo"] if val ==...
1 vote
1 answer
3k views
Remove duplicates from map of slices
I have a map of slices that I need to remove duplicates from. I think I'm close to the solution but I'm missing something that I can't quite figure out. Expected output: map[key1:[1 2 3] key2:[1 2 3]]...
0 votes
1 answer
956 views
How display missing keys in golang by looping through JSON?
I am building a translation tool for my app. I have 2 different json files (en.json, fr.json), where my keys and values are. The en.json file is the referent file which means the keys present in ...
0 votes
2 answers
70 views
Map item test as an expression
A Tour of Go explains how to test that a key is present in the map: m := make(map[string]int) m["Answer"] = 42 v, ok := m["Answer"] if ok { Do Something if set } if !ok { Do Something if not set } Is ...
0 votes
1 answer
64 views
Avoiding need for redundant check to map given zero value of string?
We have a map[string]string, I assume that means the zero value of a string retrieved from the map is "" So doesn't that mean that this: var userId, ok = params["user_id"]; if !ok || userId == "" { ...