10 examples of 'golang get map keys' in Go

Every line of 'golang get map keys' 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
768func (m *Expr_CreateStruct_Entry) GetMapKey() *Expr {
769 if x, ok := m.GetKeyKind().(*Expr_CreateStruct_Entry_MapKey); ok {
770 return x.MapKey
771 }
772 return nil
773}
359func getMapKeys(m map[string]interface{}) []string {
360 keys := make([]string, 0, len(m))
361 for k := range m {
362 keys = append(keys, k)
363 }
364 return keys
365}
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}
55func (s *StorageRedisHashTable) GetMap(id string) map[string]interface{} {
56 r, err := s.redis.DoVar("HGETALL", s.key(id))
57 if err != nil {
58 return nil
59 }
60 array := r.Interfaces()
61 m := make(map[string]interface{})
62 for i := 0; i < len(array); i += 2 {
63 if array[i+1] != nil {
64 m[gconv.String(array[i])] = gconv.String(array[i+1])
65 } else {
66 m[gconv.String(array[i])] = array[i+1]
67 }
68 }
69 return m
70}
66func GetMap(x interface{}, keys interface{}) interface{} {
67 return getMapK(x, toInterfaceArray(keys))
68}
174func getKeysOfMap(ori map[string]string) []string {
175 keys := make([]string, len(ori))
176 i := 0
177 for key := range ori {
178 keys[i] = key
179 i++
180 }
181 return keys
182}
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}
13func main() {
14 valueOf := reflect.ValueOf
15 m := map[string]int{"Unix": 1973, "Windows": 1985}
16 v := valueOf(m)
17 // A zero second Value argument means to delete an entry.
18 v.SetMapIndex(valueOf("Windows"), reflect.Value{})
19 v.SetMapIndex(valueOf("Linux"), valueOf(1991))
20 //Please note that, the MapRange method is supported since Go 1.12.
21 for i := v.MapRange(); i.Next(); {
22 fmt.Println(i.Key(), "\t:", i.Value())
23 }
24}
41func testMap() {
42 m1, m2 := make(map[int]int), make(map[int]int)
43 m := reflect.ValueOf(m2)
44 baseTest(func(i int) { m1[i] = i }, func(i int) {
45 v := reflect.ValueOf(i)
46 m.SetMapIndex(v, v)
47 })
48 fmt.Printf("normal %d\n", len(m1))
49 fmt.Printf("reflect %d\n", len(m2))
50}
51func getMapKeysVals(m map[string]interface{}) (keys []string, vals []interface{}, err error) {
52 // If invalid map, return error now.
53 if m == nil {
54 return nil, nil, errors.New("Input map cannot be nil")
55 }
56
57 // Create keys and vals slices with appropriate length/capacity.
58 n := len(m)
59 keys = make([]string, n)
60 vals = make([]interface{}, n)
61
62 // Iterate through map keys/vals and place into slices.
63 i := 0
64 for key, val := range m {
65 keys[i] = key
66 vals[i] = val
67 i++
68 }
69
70 // Return final keys/vals with success.
71 return keys, vals, nil
72}

Related snippets