> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Go Short Variable

A short variable declaration uses the `:=` operator to declare and initialize one or more local variables simultaneously, relying on the compiler to infer their types from the assigned values. It serves as a syntactic shorthand for a standard `var` declaration with an initializer, omitting the explicit type signature.

## Syntax

```go theme={"dark"}
identifier := expression
identifier1, identifier2 := expression1, expression2
```

## Lexical Scope Restrictions

Short variable declarations are strictly confined to block scope. They can only be utilized within the body of a function. Package-level variables must be declared using the `var` keyword; attempting to use `:=` at the package level results in a compile-time syntax error (`non-declaration statement outside function body`).

## Type Inference Mechanics

The compiler resolves the type of the newly declared variable at compile time based on the right-hand side (RHS) expression.

* If the RHS is a typed expression, the variable inherits that exact type.
* If the RHS is an untyped constant, the variable assumes the default type for that constant class: `int` for integers, `float64` for floating-point numbers, `complex128` for complex numbers, `rune` for character literals, `string` for string literals, and `bool` for boolean constants.

```go theme={"dark"}
package main

import (
	"fmt"
	"os"
)

func main() {
	// Type inferred as int (default for untyped integer constant)
	count := 10 

	// Type inferred as float64 (default for untyped float constant)
	pi := 3.14  

	// Type inferred as bool (default for untyped boolean constant)
	isActive := true

	// Type explicitly derived from the RHS function return types
	file, err := os.Open("data.txt") 

	// Variables evaluated to satisfy the compiler's unused variable constraint
	fmt.Println(count, pi, isActive, file, err)
}
```

## Redeclaration and Assignment Rules

Unlike standard `var` declarations, the `:=` operator permits the redeclaration of existing variables within the same lexical block, subject to a strict condition: **at least one non-blank variable on the left-hand side (LHS) must be newly declared.** The blank identifier (`_`) does not satisfy the requirement for a new variable.

When this condition is met, the `:=` operator acts as a standard assignment for the previously declared variables and a declaration for the new ones. The redeclared variables must retain their original type.

```go theme={"dark"}
package main

import "fmt"

func main() {
	// 'a' and 'b' are newly declared
	a, b := 1, 2 

	// Valid: 'c' is new. 'a' is merely reassigned.
	a, c := 3, 4 

	// Invalid: The blank identifier (_) does not count as a new variable.
	// a, _ := 5, 6 // Compile error: no new variables on left side of :=

	// Variables evaluated to satisfy the compiler's unused variable constraint
	fmt.Println(a, b, c)
}
```

## Variable Shadowing

Because `:=` always constitutes a declaration (for at least the new variables on the LHS), using it within a nested lexical block—such as an `if`, `for`, or `switch` statement—will instantiate a new variable. This new variable will shadow any variable with the identical identifier in an outer scope for the duration of the inner block.

```go theme={"dark"}
package main

import "fmt"

func main() {
	x := 10 // Outer scope 'x'

	if true {
		x := 5 // Declares a NEW 'x' in the inner scope, shadowing the outer 'x'
		x++    // Modifies the inner 'x' (becomes 6)
		
		fmt.Println(x) // Prints 6
	}

	// Outer 'x' remains 10
	fmt.Println(x) // Prints 10
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Go Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
