> ## 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 Variable

A variable in Go is a storage location holding a value of a specific, statically-enforced data type. While variables are typically bound to identifiers via declarations, they can also be unnamed (such as memory allocated via `new(T)` or through composite literals). Go is a statically typed language, meaning the type of a variable is resolved at compile time and cannot be altered during runtime.

## Declaration Syntax

Go provides multiple syntactic constructs for declaring variables, catering to explicit typing, type inference, and block-level grouping.

**1. Explicit Declaration (`var` keyword)**
Declares a variable with an explicit type. If no initial value is provided, Go automatically assigns the type's zero value.

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

import "fmt"

func main() {
    var count int
    fmt.Println(count) // Output: 0
}
```

**2. Type Inference**
When an initial value is provided, the compiler infers the type from the right-hand side expression. The explicit type declaration can be omitted.

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

import "fmt"

func main() {
    var threshold = 10.5 // Inferred as float64
    fmt.Printf("%T\n", threshold) // Output: float64
}
```

**3. Short Variable Declaration (`:=`)**
A shorthand syntax available *only* within function bodies. It implicitly declares and initializes the variable, inferring its type. It cannot be used for package-level variables.

Crucially, a short variable declaration can **redeclare** an already-declared variable, provided the redeclaration occurs in the same lexical block, the type remains identical, and at least one **non-blank** variable on the left-hand side is newly declared.

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

import (
    "fmt"
    "strconv"
)

func main() {
    isActive := true // Inferred as bool
    fmt.Println(isActive)

    // Redeclaration mechanic
    data, err := strconv.Atoi("123") // 'data' and 'err' are newly declared
    fmt.Println(data, err)

    parsed, err := strconv.Atoi("456") // 'parsed' is newly declared; 'err' is redeclared (assigned a new value)
    fmt.Println(parsed, err)
}
```

**4. Multiple and Factored Declarations**
Multiple variables can be declared on a single line or grouped in a factored block to organize package-level state.

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

import "fmt"

// Factored declaration block
var (
    host     string = "localhost"
    port     int    = 8080
    isSecure bool
)

func main() {
    // Single-line multiple declaration
    var x, y, z int
    var a, b = 1, "string" // Mixed type inference
    
    fmt.Println(host, port, isSecure)
    fmt.Println(x, y, z)
    fmt.Println(a, b)
}
```

## The Zero Value Concept

Memory allocated for Go variables is always initialized. If a variable is declared without an explicit initial value, it is assigned a "zero value" specific to its type:

* **Numeric types** (`int`, `float64`, `byte`, etc.): `0`
* **Booleans** (`bool`): `false`
* **Strings** (`string`): `""` (empty string)
* **Pointers, functions, interfaces, slices, channels, and maps**: `nil`

## Scope and Visibility

Variable scope in Go is lexically defined using blocks. Visibility across packages is determined strictly by the capitalization of the variable's identifier.

* **Block Scope:** Variables declared within a block `{}` (e.g., functions, loops, conditionals) are accessible only within that block and its nested blocks.
* **Package Scope:** Variables declared outside of any function using the `var` keyword are visible to all files within the same package.
* **Exported (Public):** If a package-level variable begins with an uppercase letter (e.g., `var Timeout int`), it is exported and accessible by external packages.
* **Unexported (Private):** If a package-level variable begins with a lowercase letter (e.g., `var bufferSize int`), it is unexported and restricted to its own package.

## Addressability and Pointers

Variables in Go are addressable entities. The memory address of a variable can be retrieved using the address-of operator (`&`), yielding a pointer to that variable's type.

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

import "fmt"

func main() {
    var counter int = 42
    var pointerToCounter *int = &counter
    
    fmt.Println(counter)           // Output: 42
    fmt.Println(*pointerToCounter) // Output: 42
}
```

## Shadowing

A variable declared in an inner lexical block can shadow a variable with the same identifier in an outer block. The inner declaration takes precedence within its scope, temporarily obscuring the outer variable.

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

import "fmt"

var x int = 10 // Package scope

func main() {
    x := 5 // Shadows the package-level 'x'
    fmt.Println(x) // Output: 5
}
```

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