Ben Chuanlong Du's Blog

It is never too late to learn.

Map in Golang

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Tips and Traps

  1. Please refer to Ordered Map in Golang for discussions on ordered maps in Golang.
In [3]:
import "fmt"
In [1]:
m := map[string]int {
    "how": 1,
    "are": 2,
    "you": 3,
}
m
Out[1]:
map[are:2 how:1 you:3]
In [6]:
m["are"]
Out[6]:
2 true
In [3]:
val, found := m["are"]
In [4]:
val
Out[4]:
2
In [5]:
found
Out[5]:
true

Iterate a Map

In [10]:
ports := map[string]int {
    "jupyterlab": 8888,
    "jupyterhub": 8000,
    "gitpod": 8000,
    "vscode": 8080,
}
for prefix, port := range ports {
    fmt.Printf("%s: %d\n", prefix, port)
}
gitpod: 8000
vscode: 8080
jupyterlab: 8888
jupyterhub: 8000

Comments