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 172 views
172 views asked Feb 29, 2020 by avibootz
1 answer 101 views
4 answers 382 views
382 views asked Feb 21, 2020 by avibootz
1 answer 103 views
1 answer 92 views
...