Go•Variables
Go Short Variable Declaration Operator (:=) and Scoping Mechanics
In Go, the short variable declaration operator (:=) provides concise shorthand to declare and initialize local variables simultaneously, allowing the compiler to infer static types from right-hand expressions. Unlike var, := is strictly restricted to function bodies and requires at least one newly declared variable on the left side.
Syntax
clusterID := "us-east-prod"
nodeCount, throughput := 16, 94.85
_, err := fmt.Println(clusterID)
Parameters & Values
Key components and acceptable values for this structure:
| Parameter | Type | Description |
|---|---|---|
| := | Short Declaration Operator | Declares and infers static types for one or more new variables inside function scope. |
| _ | Blank Identifier | Discards unwanted values to satisfy Go zero-unused-variable compiler rule. |
| var | Keyword | Standard variable declaration usable at package scope or with zero-value initialization. |
Ready-to-Run Code Example
Executable in CompilraRun this complete, working example directly without installing Go locally:
package main
import "fmt"
// Package-level variable (var is mandatory here; := is illegal)
var EnvironmentTier = "Production"
func main() {
// 1. Short variable declaration with automatic type inference
clusterName := "compilra-us-east-1"
activeNodes := 16
loadFactor := 0.732
isHealthy := true
// 2. Multi-variable assignment via :=
ingressPort, egressPort := 8080, 8443
fmt.Println("=== Go Short Variable Declaration Pipeline ===")
fmt.Printf("Environment: %s (Package Scope)\n", EnvironmentTier)
fmt.Printf("Cluster: %s (Type: %T)\n", clusterName, clusterName)
fmt.Printf("Nodes: %d (Type: %T)\n", activeNodes, activeNodes)
fmt.Printf("Load Factor: %.3f (Type: %T)\n", loadFactor, loadFactor)
fmt.Printf("Healthy: %t (Type: %T)\n", isHealthy, isHealthy)
fmt.Printf("Ingress/Egress Ports: %d / %d\n", ingressPort, egressPort)
// 3. Redeclaration in multi-variable context (activeNodes reassigned, newNodes declared)
activeNodes, newNodes := 24, 8
fmt.Printf("Scaled Nodes: Total=%d, Added=%d\n", activeNodes, newNodes)
}Expected Output:
=== Go Short Variable Declaration Pipeline === Environment: Production (Package Scope) Cluster: compilra-us-east-1 (Type: string) Nodes: 16 (Type: int) Load Factor: 0.732 (Type: float64) Healthy: true (Type: bool) Ingress/Egress Ports: 8080 / 8443 Scaled Nodes: Total=24, Added=8
How It Works: Step-by-Step
- Line 6: Declares package-level variable using var (:= is disallowed outside functions).
- Lines 10-13: Uses := inside main() to declare and infer string, int, float64, and bool types.
- Line 16: Performs simultaneous multi-variable declaration of ingressPort and egressPort.
- Line 26: Reuses activeNodes alongside newNodes: valid because at least one new variable is created.
Key Concepts
- The short variable declaration operator := can only be used inside function bodies, never at the package level.
- In a multi-variable := assignment, existing variables can be reassigned as long as at least one variable is brand new in that scope.
- Go enforces zero-unused-variable compilation rules: any local variable declared with := must be used, or the compiler halts.
- Type deduction is strictly static: float literals infer as float64, integer literals infer as platform-dependent int (32 or 64 bit).
Common Mistake: Attempting to use := at package level outside functions
The := operator is syntactically invalid outside of function bodies. Package-level declarations must always use var.
❌ Incorrect
package main serviceName := "gateway" // Compile Error: non-declaration statement outside function body
✅ Correct
package main var serviceName = "gateway" // Valid package declaration
Related Go Tutorials & Compiler
Go Operators: Arithmetic, Comparison, and Bitwise Logic
Explore arithmetic, logical (&&, ||), comparison, and bitwise operators in Go.
Go Console I/O: fmt.Print, fmt.Println, and fmt.Printf
Format output and read inputs using Go standard library fmt package.
Go Type Conversion and strconv Package (Atoi, Itoa)
Convert numeric types and parse strings using strconv.Atoi and strconv.Itoa.
Go Online Compiler
Open in Compilra IDE
Run and test Go on Compilra
Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.