How can you make a type print itself automatically.
Let me explain what I mean.
Let me show you a quick example.
Now I am on the list type and if I go and run this code it prints the products.
But what if I want to print the products using the plain old printer function.
So I'm going to add a printf here and print each product using its value so if I go and run this again
What's this.
These are the fields data of the product.
Each one of these is a product value.
So how can I print a product without calling its print method.
Let's go ahead and talk about this.
We're going to use an interface called Stringer to print a product automatically as a string.
This interface customizes the way a type represents itself as a string.
All of the printing functions in the fmt package can automatically call the string method.
In other languages there are also similar ways to print a type as a string the stringer interface is
not the only famous interface.
There are a lot of other well-known interfaces in go standard library.
We're going to talk about them in the later lectures.
No worries.
All right.
Let's talk about how we can implement the stringer interface for the product type here.
We were printing it using print.
And it was printing some weird output here.
So I'm going to go ahead to the product type here.
I just need to change the name of this method to a string.
It needs to return a String as well here.
It prints itself instead of this.
I'm going to call the sprinf function.
Sorry and I'm going to return a String value from Sprintf. By the way the list already prints a new
line here.
So I'm going to remove this new line from here.
All right let's try it again.
Awesome.
Right now we can print product values directly.
We don't have to call print method it's because print f knows that the product has a string method and
calls this method automatically.
So it can print the product automatically.
So of course we're not going to stop here and we're going to implement the string method the string
or interface in our other types as well.
You know this price field is a money type.
And here I'm calling the string method to print a money value.
Also in here the released field is a timestamp.
And I also need to call the string method here.
So let me remove these string methods and just to show you what happens when I do so let's save this
file and let's retry it as you can see the money type cannot print itself as well as the timestamp
type.
So I'm going to go to the money type and make it a stringer it's easy here.
It's because it already has a string method that returns a string.
I just need to change the first letter to s uppercase as you know interfaces in go are case sensitive.
That's why I need to do so.
So if I go back and run this again you can see that
Now the money type can print itself.
So let's do the same thing for the timestamp type.
I'm going to go to the time stamp and I'm going to change the first letter to s.
Just like I did in the money type.
All right.
So if I go to the terminal and run this code again you can see that the timestamp can print itself.
Awesome right.
Nice.
The money and timestamp types are now stringers.
They know how to print themselves.
What's left.
Actually there is also a list type you know.
And I want to make this list type also as Stringer.
So how can I do that first.
I'm going to change the method name to string and it's going to return a String value of course.
And here it prints a message.
But I want to return a string here.
So I'm going to return a String.
directly in here also.
I'm going to add a new line character here.
Now here I need to return these product values as a single string value.
To do that I'm going to use a string builder.
This is a buffer for string values.
Actually I could have combined these string values myself but that will be very inefficient if I'm going
to have multiple string values in the future.
So it is better to be ready for it.
That's why I'm using the string.
builder type here.
Now I need to write into the buffer using this string value.
So I'm going to call string.
I'm going to call the write string method.
It's here.
Right.
It takes a string value but I don't care what it returns.
So I'm just going to call the write string method and I'm going to pass it this string value.
Next I need to print the product value.
So I'm going to call the write string method again and I'm going to pass it in a product value but it
doesn't work the product value is not a string value.
So I just need to call the string method here.
As you can see not every other type knows about the stringer interface so the string builder doesn't
know anything about the stringer interface.
So I just needed to call the string method here myself.
And next I'm going to call the write rune method this time as you can see.
This time it takes a rune value.
So I'm doing this because I want to print a new line just like in here.
So that's why I called this write rune method.
So I'm going to pass it in a new line and that's it.
Now I can delete these lines of code.
All right finally I need to return a single string value so I'm going to say return and I'm going to
say string here.
I need to call the string method because it returns a string value.
So you see the strings builder is also a stringer.
So if you need too.
You can also pass this builder to printing functions directly.
There is only one problem left and it's in the main function here.
It tries to call the print method but it doesn't exist anymore.
So instead I'm going to print the list directly like this.
So if I go back to my terminal and run this code it works as before.
Nice isn't it.
So how does the stringer work behind the scenes.
Let me show you an example.
I'm going to comment out this code and I'm going to declare a new money variable pocket money.
With $10.
So I'm going to print I have pocket.
Okay.
Now if I go to my terminal and run this code you can see that I'm not that rich.
And I have only ten dollars.
So how does the println function can print this money type this money value.
Let's see how it works.
First the program executes the print with a money value the money type has a string method the print
function uses a type assertion and sees that the money is a stringer therefore it executes the string
method of the money the string method of the money returns a string value.
Finally the print function prints ten dollars.
So basically it uses a type assertion behind the scenes.
This is how it works so previously you learned how to use interfaces to group types by behavior.
You learned that you don't always have to create interfaces by yourself.
Instead you can satisfy existing interfaces existing interfaces can become bridges between your types
and code that is written by others the most significant benefit of using common interfaces is reusability
therefore most Go programmers prefer to implement methods of existing interfaces.
That's all for now.
You may pause the video here and take a look at the summary.