Variable Declarations, Short Variable Syntax (:=) & Scope
1Concept
Inside functions, := declares and infers variable types. Outside functions, var is required. Variables are block-scoped {}.
2Architecture Diagram
score := 100 // Short declaration inside function var GlobalConfig = "Active" // Package-level declaration
3Code Example
Stage 0 Language Foundations
package main
import "fmt"
const AppName = "CareerAI High-Speed Engine"
func main() {
shortDeclared := "Inferred String"
number, valid := 42, true // Multiple assignment
fmt.Println(AppName)
fmt.Printf("Short: %s, Number: %d, Valid: %t\n", shortDeclared, number, valid)
}4Expected Output
CareerAI High-Speed Engine Short: Inferred String, Number: 42, Valid: true
5Key Takeaways
- ✓:= is only valid inside function bodies.
- ✓Capitalized names (AppName) are exported (public); lowercase names are private to package.
- ✓Multiple variables can be declared and initialized on a single line.