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.

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:

  1. Converted the byte array to a string or array of strings

  2. Joined the string array together into a single string, where each element was separated by a dot/period, as in the example below

  3. 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 tomorrow’s 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, tomorrow!

#learninggolang


You might also be interested in...


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