9 examples of 'golang map' in Go

Every line of 'golang map' 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
102func (v *BaseApiParserVisitor) VisitMapType(ctx *MapTypeContext) interface{} {
103 return v.VisitChildren(ctx)
104}
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

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}
127func (e *encoder) mapv(tag string, in reflect.Value) {
128 e.mappingv(tag, func() {
129 for _, k := range in.MapKeys() {
130 e.marshal("", k)
131 e.marshal("", in.MapIndex(k))
132 }
133 })
134}
1918func (slice stringSlice) Map(functors ...stringFunctorForMap) stringSlice {
1919
1920 tmpSlice := slice
1921
1922 for _, f := range functors {
1923 if f == nil {
1924 continue
1925 }
1926 tmpSlice = MapStr(f, tmpSlice)
1927 }
1928
1929 return tmpSlice
1930}
777func (m *DtUpdateResp) GetMapValue() []*MapEntry {
778 if m != nil {
779 return m.MapValue
780 }
781 return nil
782}
1775func (x *NameSpace) GetMapType() string {
1776 if x != nil {
1777 return x.MapType
1778 }
1779 return ""
1780}
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}
317func (b *TypeBuilder) Map() PendingMap {
318 return pendingMap{b.add(&Type{kind: Map})}
319}
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}

Related snippets