Use the := operator inside the if statement's initialization clause to declare a variable scoped only to that block.
Use the short variable declaration operator := inside the if statement's initialization clause to declare and initialize a variable that is only visible within the if block.
if err := myFunc(); err != nil {
log.Fatal(err)
}
This pattern keeps the scope of err limited to the if statement, preventing it from leaking into the outer function scope.
Using If with a short statement in Go lets you create a temporary variable just for a specific check. It is like asking a question and only keeping the answer while you decide what to do next. Once the decision is made, the answer disappears, keeping your code clean.