Learning Golang, Day 11 – I Solved the Stringers Exercise!
Here we are on day 11, where I solved the Stringers Exercise.
Here we are on day 10. Today I read about and played with Type Assertions, Type Switches, and Stringers in Go.
I enjoyed the session, as it, on the whole, made sense. Type assertions and switches were pretty self-explanatory, and something that I look forward to using. The same goes for Stringers, which remind me a lot of PHP’s magic __toString function.
Because of that comparison, they’re what I want to focus on for just a bit.
From what I understand, to implement a Stringer, all you have to do is to add a method to a Type in Go named String
that returns a string
.
In PHP, if a class implements the __toString
method, which returns a string, then the object can be used as a string.
Here’s an example to demonstrate what I’m talking about.
<?php
class User
{
public function __construct(protected string $firstName, protected string $lastName){}
public function __toString(): string {
return sprintf("%s %s", $this->firstName, $this->lastName);
}
}
$user = new User("Matthew", "Setter");
echo $user;
In this example, when the code is run, the string "Matthew Setter" will be printed to the terminal.
However, when I started working through the Stringers exercise, things started to come undone. You can see the sample code below.
package main
import "fmt"
type IPAddr [4]byte
// TODO: Add a "String() string" method to IPAddr.
func main() {
hosts := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for name, ip := range hosts {
fmt.Printf("%v: %v\n", name, ip)
}
}
I looked at the example and thought that I’d just have to create a String()
method with a pointer receiver to the IPAddr
type, that:
Converted the byte array to a string or array of strings
Joined the string array together into a single string, where each element was separated by a dot/period, as in the example below
Returned the string
func (ipa IPAddr) String() string {
var output []string
for _, v := range ipa {
output = append(output, string(v))
}
return strings.Join(output, ".")
}
That didn’t work, as it converted the byte values to strings, not a string representation of the byte’s value
I don’t know if I explained that correctly.
What I mean is that, for example, it converted 127 to the DEL
char, and 0 to the NULL
char, instead of converting 127 to "127" and 0 to "0", etc.
I hope that makes sense.
I kept playing further but didn’t really make a lot of progress. To try and get "unstuck", I asked a question in the newbies channel in gophers.slack.com and am currently having a discussion about it. I hope to work through this and, in the next session, be able to solve the exercise.
What’s more, I am happy for having gotten stuck, as it’s helped me to start to appreciate a wider array of types that I’d been used to for quite some time now.
See you, next time!
Here we are on day 11, where I solved the Stringers Exercise.
Today, on day 14, I created a custom method to remove code duplication creeping into the weather station codebase. Come read the fun story behind getting that done.
Here we are on day 13. Today, I continued learning Golang by working on the Golang version of my PHP/Python weather station, adding a function to render static pages. Let me share my learnings with you.
Here we are on day 12. I didn’t solve anything. I’m feeling that these exercises are becoming arbitrary and pointless.
Please consider buying me a coffee. It really helps me to keep producing new tutorials.
Join the discussion
comments powered by Disqus