Up until now you've been using interfaces however you might not want to use them for the sake of using
them so in this lecture we're going to simplify the program that we wrote in the last lecture we're
going to use concrete types instead of interfaces.
It's because it is easier to work with them and then we're going to create a new type called timestamp.
It'll help us to make our code simpler by separating the responsibilities.
Lastly you're going to discover the power of embedding.
Let's take a look at these types.
They all are embedding a product and they have the same set of fields and methods.
However they don't have custom fields or behaviors on their own.
So they bring unnecessary complexity.
Only the book has an additional field just because of that field.
I need to use an interface so that I can group all these types.
Let's simplify the current design here.
I combine all the products into one product type in this way.
Our code will become much easier to understand and work with.
All right let's get started.
Like I said I want to combine all these products in a single product type to do that.
I'm going to go to the book type and I'm going to move this published field to the product type and
then I'm going to move this format function to the product type again I will use this format function
to format the publishing date.
OK.
By the way this published field doesn't sound right to me so I'm going to change it to released.
It's more meaningful for every kind of products.
Now I need to find a way to print this field to do that.
I'm going to add another verb here and I'm going to call the format function with the product released
field I also want to simplify the output a little bit so I'm going to delete this verb from here.
Okay now it's time to get rid of all of the other product types.
So I'm going to select book game puzzle and toy and I'm going to delete them.
Bye bye.
Let's open up the list type right now.
The list is a slice of item values.
However there is only one product type right now.
So I'm going to delete this and I'm going to make it a slice of products.
I'm going to delete this interface type as well.
Why did I use a pointer here?
It's because the product type is using methods with pointer receivers.
That's why I also want to change these I.T. variables to P. short for a product.
Now let's go back to the main function and fix these errors first.
I'm going to remove all of these products from here and I'm going to move the release date into the
product literal I don't want to move this nil release date into the product it's because I don't
want to type nil values for every other products here there is an easier way if I add the field names
back I won't need to add a released field for the empty release dates so I'm going to add titles and
I'm going to add the price fields back here I only need to add the released field name here
and here.
I don't need the type nil values now because I'm using field names I love concise variable names
so I'm going to change the name of the store to l. OK. Showtime!
Let's try this one.
Great.
It works.
I want to make it simpler.
So I'm going to delete the rest of these products from here.
We don't need them.
We can only work with these product values.
All right.
I think the code is much simpler now.
There is only one product type which has this one and there is a list of products.
That's it.
It's very simple.
We are not done yet.
Let's take a look at the product type it knows how to print a release date.
I think it knows too much.
Let's move this responsibility to another type.
Here it is the timestamp with only one responsibility to format and print timestamps.
So the released field will be a timestamp and the product will become much simpler.
Let's jump back into the code and let's create a new file timestamp.
.go and let's make a new strike type timestamp struck at timestamp is basically a time value.
So I'm going to embed the time.
This way I can use the methods of the time type.
Like I said the timestamp knows how to format and print itself.
So let's attach a string method.
T S timestamp string which returns a string.
Now you're going to see why I am using embedding.
For example let's say the timestamp is not set yet.
So how can I detect that.
Well I can use the zero method of the time.
So I'm going to call T S is zero.
This method reports whether the time value represents the zero time instant.
So I can say that the timestamp is empty.
In that case I'm going to return.
Unknown otherwise I'm going to format and return the timestamp as a string.
So first I'm going to create a time layout and I'm going to format the timestamp using the layout.
And I'm going to return it.
Now you see why I'm using embedding this way I can use the methods of the time type.
That's awesome.
All right.
The next thing that I want to do is to use this Timestamp Type from the product.
So let's go to the product and I'm going to change this
this empty interface to timestamp.
That's it.
But the product is still using the format function to format release dates.
So I'm going to delete this format from here and I'm going to call the string method instead.
Now the responsibilities are clear the product doesn't care half the format.
Release dates.
All right.
Let's go back to the main function.
There are a few problems here.
The released field is now at timestamp so it cannot contain arbitrary values like these.
So I need to convert them to timestamp values to do that.
Let's use a helper function to timestamp.
I'm also going to add one more here.
That's it.
Of course this function doesn't exist yet.
So let's go to the product and create it.
But I don't have to declare a new function.
Instead I'm going to use the format function.
I'm just going to change its name to to timestamp it so to return a new timestamp
I made it a named (result) value.
This way I can use the timestamp's zero value since.
Now we are returning a timestamp.
I'm going to remove this clause from here.
So now I need to return a timestamp value.
But now this code converts an integer which is this one into a time value but now I need to return a
timestamp.
So I'm going to delete this layout from here and I'm going to set this time value to the time field
of the timestamp.
And I'm going to directly return the timestamp.
I don't need to format it.
It's because the timestamp already knows how the format and print itself.
So here I just need to convert the integer into a timestamp.
That's it.
By the way let's also move this method from here and save this into the timestamp.
This is a better place for this function all the timestamp related things are happening here.
And if I run this you can see that Moby Dick and Odyssey get a release date.
So the timestamp formats these release dates because the Moby Dick has an integer release date and Odyssay
has a string release date.
So in this switch statement they get converted into integer values but for the hobbit it says unknown.
So it doesn't match anything in here and it gets a zero time value from the beginning and then we set
it into the timestamp and return the timestamp so the timestamp becomes zero.
So when we want to print it with the string method so the if statement here catches that it's a
zero timestamp then it returns unknown.
All right let's summarize.
We have two types product and timestamp.
Let's say I want to create a product value with a particular release date.
But the released field is a timestamp to get a timestamp value.
I call the to timestamp function then it returns me a timestamp value.
Now I can put that value to the released field of the product.
Let's say I want to print the product to do that.
I just need to call the print method but the product doesn't know how to print timestamp values.
Only the timestamp knows how to do that.
So it calls the timestamp's string method and asks the timestamp to print itself in the end the product
prints itself and the release date.
The product was formatting and printing.
The release dates by itself.
Now it is much simpler.
The released field knows of the format and print itself.
All right.
That's all for now.
You may pause the video now and take a look at the lecture summary.