Skip to main content

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.

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

import "fmt"

var x int = 10 // Package scope

func main() {
    x := 5 // Shadows the package-level 'x'
    fmt.Println(x) // Output: 5
}
Master Go with Deep Grasping Methodology!Learn More