Files
bootdotdev-fcc-learn-golang…/course/2-variables/exercises/11-if_init/readme.md
wagslane 9be3074de6 first
2023-05-01 15:25:27 -06:00

780 B

The initial statement of an if block

An if conditional can have an "initial" statement. The variable(s) created in the initial statement are only defined within the scope of the if body.

if INITIAL_STATEMENT; CONDITION {
}

Why would I use this?

This is just some syntactic sugar that Go offers to shorten up code in some cases. For example, instead of writing:

length := getLength(email)
if length < 1 {
    fmt.Println("Email is invalid")
}

We can do:

if length := getLength(email); length < 1 {
    fmt.Println("Email is invalid")
}

Not only is this code a bit shorter, but it also removes length from the parent scope, which is convenient because we don't need it there - we only need access to it while checking a condition.