Linked Questions

0 votes
1 answer
17k views

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) ...
sam's user avatar
  • 19.3k
3 votes
1 answer
3k views

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[...
user avatar
0 votes
1 answer
1k views

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 ...
jiyinyiyong's user avatar
  • 4,773
0 votes
0 answers
521 views

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]...
Rakesh Gupta's user avatar
132 votes
9 answers
90k views

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 ...
Kansuler's user avatar
  • 1,589
108 votes
1 answer
59k views

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 ...
marcio's user avatar
  • 10.6k
53 votes
3 answers
39k views

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 ...
maerics's user avatar
  • 157k
16 votes
3 answers
40k views

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 ...
Maiko Ohkawa's user avatar
5 votes
3 answers
20k views

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 :)
Dinesh Auti's user avatar
11 votes
2 answers
8k views

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 ...
wrwr's user avatar
  • 145
2 votes
2 answers
3k views

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 ...
ydhhat's user avatar
  • 391
3 votes
2 answers
3k views

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 ...
user avatar
4 votes
2 answers
4k views

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 ==...
user avatar
1 vote
1 answer
3k views

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]]...
poldy's user avatar
  • 204
0 votes
1 answer
956 views

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 ...
Shirley Truffier-Blanc's user avatar
0 votes
2 answers
70 views

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 ...
exebook's user avatar
  • 34.5k
0 votes
1 answer
64 views

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 == "" { ...
user avatar