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
...