Welcome! In the previous lectures, you've seen that "Switch" statement is very similar to an "if" statement.
right?
So there should be an else branch inside a switch statement.
as well, right?
Yes! that's right.
Let's check it out.
To keep it simple I removed the other case clauses from here.
Now let's say that the user has requested an unknown city name
So you want to display a message to the user that the program doesn't know about that city.
Let's take a look at how to do that. first,
I'm going to change the name of the city to something unknown like Limbo.
You may remember this name from the inception movie great movie by the way.
Right.
Anyways, now I'm going to add a default clause here, like this.
So when no case is matched to the given city and this default clause will be executed instead. OK let's
print the message when the program can't find the city like this. OK!
That's it.
Now let's take a look at this.
in the coding editor. right
coding time as usual.
Here it is. OK!
First, let me write without a default clause. `go run main.go Limbo`. as you can see it prints nothing.
when I give it Limbo.
It's because none of the cases matched to limbo.
That's normal.
Now lets add the default clause to the switch. Here I'm gonna type default with an ending colon
like the case clauses like this.
then I am gonna print a message. fmt.Println("Where?")
All right.
Now let's run it again.
as you can see this time it's prints where. here none of the cases matched to limbo.
Right.
There are only Paris and Tokyo.
So since it couldn't find a matching case and this default clause is executed instead. its that simple.
by the way, you can move the default clause to anywhere inside the switch statement like this.
It'll work the same.
All right, Lets try it. As you can see it prints where again.
Now this right part is this time it brings France.
It still works! as you can see the default clause still runs the last.
So it's location in the switch statement doesn't matter.
By the way, let me also tell you that the else branch in if statement there can be only one
default clause.
per statement. Let me type another default clause here. as you can see go warns me that there
are multiple defaults here.
So it doesn't allow me to do so.
Let's take it back.
So in summary the default clause is just like an else branch.
in an iif statement that can be only one default clause and it will be executed if none of the cases
match to the switch condition.
All right.
That's all for now.
Thank you for watching.
See in the next lecture, Bye Bye.