Welcome back.
When you have a lot of conditions to check you can also use a type switch.
Let me show you how it works.
This is what a type switch looks like it detects and extracts the dynamic value from an interface
value.
It works just like a type assertion but in a switch statement when you say dot type within parentheses
the switch statement becomes a type switch.
So you can use it to extract dynamic values from interface values.
The V should be an interface value so the type switch can extract the dynamic value from the interface
on the left side there is a variable the type switch assigns to extract the value to this variable.
This is the interesting part.
The type of this variable will change depending on the extracted value.
For example let's say you want to extract an int value.
In the case statement the variable becomes an int variable or let's say you want to extract a string
value.
So the variable e becomes a string variable.
You can also use a default case if the value in the interface is neither an int or a string.
The type switch won't extract anything.
Instead it will assign the given interface value to the variable e in summary unlike a regular switch statement
type switch compares types rather than values cases match actual types inside the given interface
value.
Let's take a look at an example.
So you can understand it better I was working on the book type.
There are a lot of type checkings here.
Let's use a type switch instead.
First I'm going to type a switch statement.
I want to extract the type inside the empty interface.
So I'm going to type `v.(type)`.
In this case the type switch will only tell us the type inside the interface.
But it won't extract the value for now let's use it this way.
I'm going to add an int case and a string case.
I will also add a default case.
Now let's print the type of the value inside each condition
let's try it.
Moby Dick has an integer publishing date so that int case matches and it prints int Odyssey has a string
date.
So the string case prints string Hobbit doesn't have a date.
So the default case prints nil as you can see the type switch compares types not values.
Now let's remove the print functions and use it for real.
If the publishing date is nil.
Default clause will run.
So I'm going to return.
Unknown if a date is an int, the int case will run.
So I'm going to move the assignment to here if the date is a string. the string case will run.
So I'm going to move the conversion to here.
Lastly I'm going to move the T variable before the switch.
All the cases use it but it doesn't work.
Why?
Here it says V is not an int.
The next error says V is not a string.
This happens because I forgot to create a variable to solve this problem.
I'm going to declare a variable nice.
The errors are gone.
V starts as an empty interface.
If the int case executes it becomes an int variable.
Or if the string case executes it becomes a string variable.
However if no case matches in the default case it (`v`) stays the same as an empty interface. Phew!...
Now I can run the code.
Excellent.
It works.
Now let's nicely format the dates as you can see the publishing dates are looking weird.
I want them to look like this but how can I format them like this?
I have a time value.
Let's take a look at its methods.
I'm looking for the Format method.
It converts a time value to a string.
Using the given layout. Go uses a rather unusual layout format that looks like this.
I want to print a four digit year and a short month name like this.
So I need to pick these parts from the reference date and put it into my layout.
The reference year is 2006 so I'm going to type 2006 to my layout and the reference month is January.
so I'm going to type Jan to my layout instead of calling the string method.
I'm going to call the format method with my layout like this.
Let's try it.
It looks good I can change the layout whatever I want as long as I use the parts of the reference date.
Now it prints the dates with a different layout.
Cool but I want to print the month as the last.
Let's change the layout for the last time okay.
It's better in summary if you want to type assert for multiple types it can be better to use a type
switch.
Otherwise it's perfectly fine to use a simple type assertion.
See you in the next lecture.