Learning Golang, Day 10 – Type Assertions, Type Switches, and Stringers.

Learning Golang, Day 10 – Type Assertions, Type Switches, and Stringers.

Here we are on day 10. Today I read about and played with Type Assertions, Type Switches, and Stringers in Go.


:idseparator: - :idprefix: :experimental: :source-highlighter: rouge :rouge-style: pastie :imagesdir: /images

I enjoyed the session, as it, on the whole, made sense. Type https://go.dev/tour/methods/15[assertions] and https://go.dev/tour/methods/16[switches] were pretty self-explanatory, and something that I look forward to using. The same goes for https://go.dev/tour/methods/17[Stringers], which remind me a lot of PHP’s https://www.php.net/manual/en/language.oop5.magic.php#object.tostring[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.

[source,php]

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 https://go.dev/tour/methods/18[the Stringers exercise], things started to come undone. You can see the sample code below. [source,go] ---- 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 [source,go] ---- 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, link:/learning-golang/day-11[next time]!**

You might also be interested in these tutorials too...


Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus