News Stay informed about the latest enterprise technology news and product updates.

Get started with GoLang maps: A tutorial

An important part of programming with GoLang is working with maps. In this GoLang maps tutorial, we’ll demonstrate the value of maps and show you how you can integrate them into your programs.

What are GoLang maps?

A map has a key and a value. The value can be any value, such as another map. For example, here is a map of strings with a key of type int and value string. You use the make command to instantiate the map.

people := make(map[int]string)
people[1] = "Walker"

Here is a complete example. In this case, we’ll declare and then instantiate the GoLang map in two steps. Then, we use the GoLang range operator to loop over each key and retrieve the value by index. Then we print it out.

package main
import "fmt"

func main() {
  var pLangMap map[string]string

  pLangMap = make(map[string]string)

  pLangMap["Go"] = "Easy"
  pLangMap["Python"] = "Easy"
  pLangMap["Scala"] = "Difficult"
  pLangMap["Java"] = "Moderate Difficult"
  pLangMap["C++"] = "Difficult"

  for lang := range pLangMap {
    fmt.Println("How Difficult is", lang, pLangMap[lang])
  }

}

Here is the output. Notice that the keys are printed out of order. This is because Golang is multithreaded and asynchronous.

How Difficult is Scala Difficult
How Difficult is Java Moderate Difficult
How Difficult is C++ Difficult
How Difficult is Go Easy
How Difficult is Python Easy

Sorting GoLang maps

To sort the map items, we cannot simply use sort.Strings since the key is not a string.

sort.Strings(pLangMap)

cannot use pLangMap (type map[string]string) as type []string in argument to sort.Strings

So let’s make a new map whose key type is an array of strings []string and has an arbitrary value: we use len(map) which is the number of elements in the map. Then, we plug the value into the key and sort on the key with sort.Strings.

func main() {

  var pLangMap map[string]string

  pLangMap = make(map[string]string)

  pLangMap["Go"] = "Easy"
  pLangMap["Python"] = "Easy"
  pLangMap["Scala"] = "Difficult"
  pLangMap["Java"] = "Moderate Difficult"
  pLangMap["C++"] = "Difficult"

  mk := make([]string, len(pLangMap))
  i := 0
  for k, _ := range pLangMap {
    mk[i] = k
    i++
  }
  sort.Strings(mk)
  fmt.Println("How Difficult is", mk)
}

Now the elements are printed in order.

How Difficult is [C++ Go Java Python Scala]

Retrieve Value

Here is how to retrieve a map value.

pLangMap["Go"] = "Easy"
x := pLangMap["Go"]
fmt.Println("How Difficult is", x)

Test for a Key

To test for a key, use the underscore (_) to retrieve a value without using it. If we put some placeholder there like, for example, v, then Go would complain that we are declaring a value but not using it. If the key is not there, then the boolean ok will be equal to false.

_, ok := pLangMap["Got"]

if ok == false {
  fmt.Println("Got not found")
}

Map of Structs

Here we create a map of a struct of type students then print each student name:

package main

import "fmt"

type student struct {
  name string
  class string
  grades string
}

func main() {

  var students map[int]student
  students = make(map[int]student)
  students[1] = student{"Walker", "calculus", "A"}

  for k, v := range students {
    fmt.Println("key=",k, " name=" , v.name)
  }
}

Outputs:

key= 1 name= Walker

As you can see, GoLang maps are incredibly easy to work with, and it’s a simplicity that extends throughout the language, which is why GoLang is becoming an increasingly popular web programming language.

If you are interested in following along with this GoLang maps tutorial, you can paste the code provided below into the GoLang Playground.

Join the conversation

1 comment

Send me notifications when other members comment.

Please create a username to comment.

Here is the same thing in C++.  

OK, OK.  

C++ has three includes in place of one for GO. 
Cancel

-ADS BY GOOGLE

SearchCloudApplications

SearchSoftwareQuality

SearchHRSoftware

SearchSAP

SearchERP

DevOpsAgenda

Close