If Statements in Go
Go uses if, else if, and else to control program flow based on boolean conditions. Code inside a block runs only when its condition evaluates to true, and additional branches can be chained with else if and else.
Short Statements in Conditionals
Go allows an initialization statement within an if condition using a semicolon. This is commonly used for error handling, keeping variables scoped to the conditional block and improving code clarity.
Switch Statements
The switch statement provides a cleaner alternative to multiple else if statements when evaluating a single expression. It executes the first matching case, with an optional default to handle unmatched conditions.
Multiple Case Values and Shared Logic
A single case can include multiple values separated by commas. This enables grouping related conditions under shared logic, reducing duplication and improving readability.
Fallthrough Behavior
Go does not automatically fall through between cases. The fallthrough keyword must be explicitly used to continue execution into the next case, preventing unintended behavior.
Expressionless Switch
Go supports a switch without an expression, functioning as a structured alternative to chained if statements. Each case evaluates a boolean condition independently.
typically helpful to be able to control the flow of operations based on certain
criterias or conditions when you're programming in Go we can either choose
between a classic if else statement or a switch statement and if statement in Go
looks like this so let's say we have the API key variable from last episode
we will say if I key equals to constant and super secret and then whatever we
have inside this bracket will get executed if this statement evaluates to
true now we could add our L statement as well and just say if the first condition
doesn't evaluate to true then execute whatever is in the L statement finally
if we have multiple
conditions then we can execute whatever is in the L statement finally if we have multiple
conditions then we can execute whatever is in the L statement finally if we have multiple
conditions then we can execute whatever is in the L statement finally if we have multiple
conditions then we can execute whatever is in the L statement finally if we have multiple
conditions then we can execute whatever is in the L statement finally if we have multiple
conditions then we can execute whatever is in the L statement finally if we have multiple
conditions then we can execute whatever is in the L statement finally if we have multiple
conditions then we can execute whatever is in the L statement finally if we have multiple
conditions then we can execute whatever is in the L statement finally if we have multiple conditions we would like to check
conditions we would like to check
conditions we would like to check for we could use an else if so you can
for we could use an else if so you can
for we could use an else if so you can say else if
say else if
say else if let's say api key is equal to not super
let's say api key is equal to not super
let's say api key is equal to not super secret
secret
secret and then we will do something like this
and then we will do something like this
and then we will do something like this and then again if that evaluates to true
and then again if that evaluates to true
and then again if that evaluates to true and the first one does not then we
and the first one does not then we
and the first one does not then we execute whatever is in this in these
execute whatever is in this in these
execute whatever is in this in these brackets and if
brackets and if
brackets and if both of these evaluates the false then we
both of these evaluates the false then we
both of these evaluates the false then we then we execute whatever is in the else
then we execute whatever is in the else
then we execute whatever is in the else statement
statement
statement you can nest these as much as you want
you can nest these as much as you want
you can nest these as much as you want however
however
however a go developer would tell you to never go
a go developer would tell you to never go
a go developer would tell you to never go beyond two levels
beyond two levels
beyond two levels as this can lead to code that's hard to
as this can lead to code that's hard to
as this can lead to code that's hard to read and reason about
read and reason about
read and reason about finally i want to show you a more
finally i want to show you a more
finally i want to show you a more shorthand form of of doing an else if
shorthand form of of doing an else if
shorthand form of of doing an else if statement
statement
statement and that is really handy whenever we
and that is really handy whenever we
and that is really handy whenever we we only want to check a certain thing but
we only want to check a certain thing but
we only want to check a certain thing but we don't really want to use
we don't really want to use
we don't really want to use that thing later on in the program so we
that thing later on in the program so we
that thing later on in the program so we can say
can say
can say if error equals true let's say we have
if error equals true let's say we have
if error equals true let's say we have a function that validates the api key so
a function that validates the api key so
a function that validates the api key so value date
value date
value date key api key here and then if error
key api key here and then if error
key api key here and then if error does not does not equal null then
does not does not equal null then
does not does not equal null then execute whatever is inside of these
execute whatever is inside of these
execute whatever is inside of these brackets we haven't touched upon errors
brackets we haven't touched upon errors
brackets we haven't touched upon errors yet we will do that in an upcoming
yet we will do that in an upcoming
yet we will do that in an upcoming episode but the reason why this work
episode but the reason why this work here is that errors are values so we
here is that errors are values so we
here is that errors are values so we could treat them as such instead of
could treat them as such instead of
could treat them as such instead of using an else if statement to check for
using an else if statement to check for
using an else if statement to check for multiple conditions we could instead
multiple conditions we could instead
multiple conditions we could instead use what is known as a switch statement
use what is known as a switch statement
use what is known as a switch statement and a switch statement simply looks like
and a switch statement simply looks like
and a switch statement simply looks like this we specify the switch keyword and
this we specify the switch keyword and
this we specify the switch keyword and then whatever we wanna
then whatever we wanna
then whatever we wanna wanna check something about so we say api
check something about so we say api
check something about so we say api key here
key here
key here then inside the brackets we can specify
then inside the brackets we can specify
then inside the brackets we can specify cases where we want to do something so
cases where we want to do something so
cases where we want to do something so if we mimic what we had above here we
if we mimic what we had above here we
if we mimic what we had above here we can say case
can say case
can say case constant
constant
constant and super
and super
and super secret
secret
secret or
or
or another other case was
another other case was
another other case was not super
not super
not super secret
secret
secret and then finally we could say if none of
and then finally we could say if none of
and then finally we could say if none of these cases evaluated true then we have a
these cases evaluated true then we have a
these cases evaluated true then we have a default case that will
default case that will
default case that will that will then trigger if if none of the
that will then trigger if if none of the
that will then trigger if if none of the other or the cases are buffeted
other or the cases are buffeted
other or the cases are buffeted evaluated to true
evaluated to true
evaluated to true so we will simply have the logic
so we will simply have the logic
so we will simply have the logic after after the colon here
after after the colon here
after after the colon here sometimes we might have situations where
sometimes we might have situations where
sometimes we might have situations where we want to do the same thing across
we want to do the same thing across
we want to do the same thing across multiple cases so again if we take our
multiple cases so again if we take our
multiple cases so again if we take our api key here we could add another one
api key here we could add another one
api key here we could add another one and say
and say
and say if it's either constant or super
if it's either constant or super
if it's either constant or super constant and super secret or just super
constant and super secret or just super
constant and super secret or just super secret then this case here should
secret then this case here should
secret then this case here should should trigger it's important to note
should trigger it's important to note
should trigger it's important to note here that the default case is not a
here that the default case is not a
here that the default case is not a requirement
requirement
requirement also
also
each case would automatically break if it
each case would automatically break if it
each case would automatically break if it matches the expression
matches the expression
matches the expression there is no fall through behavior as you
there is no fall through behavior as you
there is no fall through behavior as you might see in other languages to have
might see in other languages to have
might see in other languages to have fall through behavior where
fall through behavior where
fall through behavior where even if something evaluates to true that
even if something evaluates to true that
even if something evaluates to true that it continues to check we will have to
it continues to check we will have to
it continues to check we will have to add the fall through keyword
add the fall through keyword
add the fall through keyword finally
finally
finally we don't even need to specify
we don't even need to specify
we don't even need to specify an expression we could just say switch
an expression we could just say switch
an expression we could just say switch and then
and then
and then have let's say we just grab
have let's say we just grab
have let's say we just grab the case from up here let me just clean
the case from up here let me just clean
the case from up here let me just clean it up
it up
it up and then we can just specify our cases
and then we can just specify our cases
and then we can just specify our cases directly so api key
directly so api key
directly so api key equals to
equals to
equals to constant and super secret
constant and super secret
constant and super secret or api key equals to
or api key equals to
or api key equals to not super
not super
not super secret
secret
secret and so on and so forth this is really
and so on and so forth this is really
and so on and so forth this is really or this is particularly helpful whenever
or this is particularly helpful whenever
or this is particularly helpful whenever you have complex conditions and you want