Check out a free preview of the full Basics of Go course

The "Implementing the Stringer Interface" Lesson is part of the full, Basics of Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates implementing a method that modifies the default output of a type to a customized string format.

Preview
Close

Transcript from the "Implementing the Stringer Interface" Lesson

[00:00:00]
>> So now on main.go, I can also create one course. Let me remove those prints for now. Let's keep the Kyle's print. So I'm going to create the course and say that the goCourse := data.Course, and I can pass from here, I can request the values. I see how I never hear why, because I'm not using goCourse, which is expectable.

[00:00:29]
But now also I can add the ID, I can add the name, okay? This is the Go Fundamentals, and I can add the instructor. And I can say the instructor is Max. What if I print the goCourse, or I call format print, okay? What I'm going to see from my structure?

[00:00:54]
I don't know, right? So what's the output of this? I'm getting an error, illegal types. So print is not actually accepting my goCourse. You can say, well, let's change to printf, or println, or printf from the format library. And then you say, why don't you show me the value of a goCourse?

[00:01:16]
Remember, %v is to represent the value, okay? So if I run this, this is how it looks like. So it looks like something, we are like a JSON without properties and commas, something like that. And then you say, yeah, can I change that? Can I change the default output of my own type?

[00:01:40]
We do, we can do that. Remember that on other languages, you typically have the toString. Well, here, instead of toString, it's called, remember that name, string. So we need on the course, that's the one that we want to change, a method. So it's a method, function that will apply to the course with a name String that returns a string, okay?

[00:02:11]
And this is a function name. So this is the name of the function. So the format print function is looking for a String function that returns a string. It sounds like a bustle, okay? But now you can return how do you want to render a course in the console.

[00:02:37]
So you can return an Sprintf with a format, saying that, I don't know, some dashes with the name. And then in parentheses, the name of the instructor, whatever. So then if I have two values, two placeholders, I will say the course.Name, and the second one, course.Instructor.FirstName. So what I'm trying to do, I'm trying to customize how do you see the output of elements.

[00:03:14]
And by the way, I should add a new line also at the end, and \n will add a new line. So now Kyle Simpson is on a different line. Does it make sense? So with this, you can, Change, it's like kind of a string representation of your structure, that's kind of idea.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now