Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
To choose between several options with "if" / "else" statements, you'd have to nest them several deep, which is hard to read. So like most programming languages, Go offers a "switch" statement. You specify the expression you want to switch on, "case" statements with possible values for that expression, and one or more lines of code that should run in each case.
- To choose between several options with
if
/else
statements, you'd have to nest them several deep, which is hard to read - So like most programming languages, Go offers a
switch
statement. - You specify the expression you want to switch on, case statements with possible values for that expression, and one or more lines of code that should run in each case.
- You can add a
default
case at the end, that will run if none of the other cases match.
package main
import (
"fmt"
)
func main() {
fmt.Print("You win: ")
doorNumber := 2
switch doorNumber {
case 1:
fmt.Println("a new car!") // not printed
case 2:
fmt.Println("a llama!") // printed
default:
fmt.Println("a goat!") // not printed
}
}
To choose between several
different options, using if and
0:00
else states, you'd have to nest some
several deep, which can be hard to read.
0:03
So like most programming languages,
Go offers a switch statement.
0:07
You specify the expression you wanna
switch on, case statements with
0:11
possible values for that expression, and
then code that should run for each case.
0:14
You can also add the default
case at the end,
0:20
that will run if none of
the other cases match.
0:22
So up here, we've initialized
the doorNumber variable with the value 2,
0:25
the integer 2.
0:30
And we're going to switch on
the value that's in doorNumber.
0:32
So we've got case statements here,
for possible values of doorNumber.
0:35
If doorNumber was set to 1,
we would print the string, a new car!
0:38
But up here, it's actually set to 2, so
0:44
it's going to match this case here,
case number 2.
0:47
And it means that his code here will run,
it will print the string, a llama!
0:51
So if we try running this down here,
we'll see, You win a llama!
0:57
Let's try going up here into the code and
setting it to 1, so
1:01
that we can look at the other case.
1:04
Try running this again.
1:07
This time, you can see we won a new car.
1:08
And if we set it to some
completely different value,
1:10
let's say we pick door number 99.
1:14
In that case none of the cases
here will match, and
1:16
therefore the default
case will run instead.
1:20
And it will print, You win a goat!
1:24
Let's change this back to doorNumber 1,
up here.
1:27
In some languages, the case after
the selected case will also run,
1:31
unless you insert a break
statement at the end of the case.
1:34
In Go,
only the selected case runs by default.
1:37
So, if we select doorNumber 1,
only doorNumber 1 runs.
1:40
But you can run the next case as well,
1:43
if you explicitly include
the fallthrough keyword.
1:46
So lets type fallthrough here,
and try re-running it.
1:51
And now, we win both a new car and
a llama.
1:55
That ends our tour of
control structures in Go.
1:58
Go's a very simple language, so
2:01
you've actually learned a substantial
part of its syntax already.
2:03
The next stage is going to be
a bit longer than this one.
2:08
We're going to look at the different
ways Go can store data, and
2:10
there are a lot of them.
2:13
We'll learn about slices,
maps, structs and more.
2:15
Learning them will take a bit of work, but
2:18
when you're done, you'll have a variety
of ways to simplify your programs.
2:19
See in the next stage.
2:23
You need to sign up for Treehouse in order to download course files.
Sign up