Here we are on day 6 – the start of the week two. Today, was a slower day, where I focused on one topic, maps!
:idseparator: -
:idprefix:
:experimental:
:source-highlighter: rouge
:rouge-style: pastie
:imagesdir: /images
:source-linenums-option: on
:tip-caption: 💡
As I sat down to continue working through https://go.dev/tour/[the Go Tour], today, I felt that this week, I’d take things slightly slower, and learn more about fewer things.
While last week was great, and I worked through quite a number of concepts, I felt that I was starting to gloss over things.
Because of that I felt that I’d progressively know a little bit about a broad range of things, without ever really having a deep knowledge of the language.
Extrapolating this out a year or so, I felt that this would, ultimately do me a disservice.
So, today, I took time to appreciate https://go.dev/tour/moretypes/19[maps], what they are, how they can be manipulated.
If you’re not familiar, https://go.dev/tour/moretypes/19[quoting the Go Tour]:
A map maps keys to values.
They’re rather like https://www.php.net/manual/en/language.types.array.php[associative arrays in PHP] and https://docs.python.org/3/tutorial/datastructures.html#dictionaries[Dictionaries in Python], if you’re familiar with those languages.
Take the following PHP example.
[source,php]
'Albury',
'ADX' => 'RAF Leuchars',
];
foreach ($airportCodes as $airportCode => $airportName) {
printf("%s -> %s\n", $airportCode, $airportName);
}
----
The code declares a variable named `$airportCodes` which is an array string keys that map to string values.
The array's key is the 3-char airport code and the array's value is the airport's full name.
After that, it uses a foreach loop to iterate over and print out the airport codes and names with `printf`.
Now, in the example below, you can see how `$airportCodes` could be defined and iterated over in Go.
[source,go]
----
airportCodes := make(map[string]string)
airportCodes["ABX"] = "Albury"
airportCodes["ADX"] = "RAF Leuchars"
for code, name := range airportCodes {
fmt.Printf("%s -> %s\n", code, name)
}
----
The `make` function is used to define a map with a string key that has a string value.
Then, two entries are added, with the same key and value as in the PHP example.
After that, https://gobyexample.com/range[range] is used to retrieve the airport code and name in the for loop and use `fmt.Printf` (with the same format string) to print them out.
Given the similarity in concept and practice, I've been able to learn the concept quite quickly and suspect that given https://go.dev/tour/moretypes/22[the seeming simplicity of mutating maps], that I'll come to love them quite quickly.
After having a play around with maps for about 15 minutes, I used the remaining 15 minutes to work through https://go.dev/tour/moretypes/23[the maps exercise], which you can see below.
I don't know that it's the most elegant of solutions, but when running it, I always received `PASS` in the console output.
Have a look and let me know in the comments what you think.
[source,go]
----
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
count := make(map[string]int)
for _, string := range strings.Fields(s) {
count[string] = updateCount(count, string)
}
return count
}
func updateCount(count map[string]int, string string) int {
if _, ok := count[string]; ok {
return count[string] + 1
}
return 1
}
func main() {
wc.Test(WordCount)
}
----
I'm not sure if I'll push on with the Go Tour, tomorrow, or just play with maps a bit longer.
They're fun and remind me a lot of PHP, so I might just play with them for a second session.
**See you, link:/learning-golang/day-7[next time]!**
Join the discussion
comments powered by Disqus