How to use an if statement with variable declaration in Go

1 Answer

0 votes
package main

import "fmt"

func main() {
    // if statement with variable declaration
    if age := 18; age >= 18 {
        fmt.Println("You are an adult.")
    } else {
        fmt.Println("You are not an adult.")
    }

    // The `age` variable is scoped to the if statement and cannot be accessed here
}


/*
run:

You are an adult.

*/

 



answered Apr 8, 2025 by avibootz

Related questions

1 answer 170 views
170 views asked Feb 29, 2020 by avibootz
1 answer 99 views
4 answers 367 views
367 views asked Feb 21, 2020 by avibootz
1 answer 102 views
1 answer 89 views
...