Maps in Go

 This is a study note from YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s

Maps can be created in Go using type map[string]int{}.
func main() {

statePopulations := map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
"New York": 19745289,
"Pennsylvania": 12802503,
"Illinois": 12801539,
"Ohio": 11614373,
}
fmt.Println(statePopulations)
}

You can also use make function to create map:

func main() {
statePopulations := make(map[string]int) // you can use make(map[string]int, 100)
                                             // to specify length as well
statePopulations = map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
"New York": 19745289,
"Pennsylvania": 12802503,
"Illinois": 12801539,
"Ohio": 11614373,
}
fmt.Println(statePopulations)
}

Like dictionary in C#, you can simply access a map value using key - statePopulations["Ohio"].

You can also add key-value pair in map like dictionary in c#:

statePopulations["Georgia"] = 10210371

You can use delete built-in function to delete a key in map:

delete(statePopulations, "Texas")

The first parameter is the map, and second parameter is the key you want to delete.

What if you access a key which does not exist in the map?

Will this give an error or exception? The answer is neither.

func main() {
statePopulations := map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
"New York": 19745289,
"Pennsylvania": 12802503,
"Illinois": 12801539,
"Ohio": 11614373,
}
fmt.Println(statePopulations["Georgia"])
}

The result is:

0

From the example above, you can see the value is 0 when key does not exist. This will cause problem if you use the value directly because you don't know "0" means key does's exist or the population is 0. However there is a solution: you can use comma separated expression to get another boolean value from map. 

func main() {
statePopulations := map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
"New York": 19745289,
"Pennsylvania": 12802503,
"Illinois": 12801539,
"Ohio": 11614373,
}

population, ok := statePopulations["Georgia"]
fmt.Println(population, ok)
}

The result is:

0 false

If you don't care about the value for now, you can simple use the write-only operator "_" to skip the value and get the true/false result only.

_, ok := statePopulations["Georgia"]
fmt.Println(ok)


Map is also a reference type, so if you have two map variable refer to the same memory, when one is modified, the other is modified as well.

func main() {
statePopulations := map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
"New York": 19745289,
"Pennsylvania": 12802503,
"Illinois": 12801539,
"Ohio": 11614373,
}
a := statePopulations
delete(a, "Ohio")
fmt.Println(a)
fmt.Println(statePopulations)
}

In above example, you can see the result that, "Ohio" is removed from both "statePopulations" and "a" referencces.



Reference: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s

Comments

Popular posts from this blog

Basic understanding of TLS-PSK protocol

Differences between ASIC, ASSP and ASIP

Orthogonal instruction set