10 examples of 'golang map key exists' in Go

Every line of 'golang map key exists' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
104func keyInMap(k string, m map[string]interface{}) bool {
105 if _, ok := m[k]; ok {
106 return true
107 }
108 return false
109}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

652func mapContainsKeyPartial(theMap map[string]interface{}, key string) (bool, interface{}) {
653 for k, v := range theMap {
654 if strings.Contains(k, key) {
655 return true, v
656 }
657 }
658 return false, nil
659}
46func MapSSValOrEmpty(data map[string]string, key string) string {
47 if val, ok := data[key]; ok {
48 return val
49 }
50 return ""
51}
465func keyExists(key string, haystack map[string]string) bool {
466 _, exists := haystack[key]
467 return exists
468}
763func mapGet(m, k, v reflect.Value) (vv reflect.Value) {
764 var urv = (*unsafeReflectValue)(unsafe.Pointer(&k))
765 var kptr = unsafeMapKVPtr(urv)
766
767 urv = (*unsafeReflectValue)(unsafe.Pointer(&m))
768
769 vvptr := mapaccess(urv.typ, rv2ptr(urv), kptr)
770 if vvptr == nil {
771 return
772 }
773 // vvptr = *(*unsafe.Pointer)(vvptr)
774
775 urv = (*unsafeReflectValue)(unsafe.Pointer(&v))
776
777 unsafeMapSet(urv.ptr, urv.typ, vvptr, refBitset.isset(byte(v.Kind())))
778 return v
779}
244func findMapInSliceBasedOnKeyValue(m []interface{}, key string, value interface{}) (map[string]interface{}, int, bool) {
245 for k, v := range m {
246 typedV := v.(map[string]interface{})
247 valueToMatch, ok := typedV[key]
248 if ok && valueToMatch == value {
249 return typedV, k, true
250 }
251 }
252 return nil, 0, false
253}
20func SetMapIfNotExists(m map[string]string, k string, v string) {
21 if _, ok := m[k]; !ok && v != "" {
22 m[k] = v
23 }
24}
9func createMap() {
10 //initialize map with key and value as string
11 var stringMap = make(map[string]string)
12
13 //add keys to map
14 stringMap["A"] = "AAA"
15 stringMap["B"] = "BBB"
16 stringMap["C"] = "CCC"
17
18 fmt.Print(stringMap)
19
20 //delete key
21 delete(stringMap, "A")
22
23 //initialize map with key and value using a map literal
24 var intMap = map[int]string{
25 1: "one",
26 2: "two",
27 3: "three",
28 }
29
30 fmt.Print(intMap)
31
32 //access item from map
33 value, ok := intMap[1] //ok is true if item exists
34 if ok {
35 fmt.Printf("Key = 1; Value =%v", value)
36 }
37}
147func exists(a []string, k string) bool {
148 for _, i := range a {
149 if i == k {
150 return true
151 }
152 }
153
154 return false
155}
235func SetMapValue(theMap map[string]interface{}, key string, val interface{}) {
236 mapValue, ok := theMap[key]
237 if !ok || mapValue == nil {
238 theMap[key] = val
239 }
240}

Related snippets