As I sat down to continue working through 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 maps, what they are, how they can be manipulated.
If you’re not familiar, quoting the Go Tour:
A map maps keys to values.
1
2
3
4
5
6
7
8
9
10
| <?php
$airportCodes = [
'ABX' => '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.
1
2
3
4
5
6
7
| 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, 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 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 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| 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.
Join the discussion
comments powered by Disqus