Variables are how we store and manipulate data in Go. They act as named containers that hold values which can change during your program's execution.
Go provides several ways to declare variables, each suited for different situations.
The most common way to create variables in Go is using the shorthand initializer (:=). This is concise and lets Go automatically determine the type based on the value you assign.
name := "Goku"
age := 30
isAlive := true
The := operator declares and initializes the variable in one step. This is the preferred method when you're inside a function and initializing the variable immediately.
Sometimes you want to be explicit about the type of your variable. You can use the var keyword followed by the variable name and its type.
var powerLevel uint
powerLevel = 9001
// Or declare and initialize in one line
var height int = 175
This approach is useful when you need to be specific about the type, such as using uint (unsigned integer) instead of the default int.
You can also use var and let Go infer the type from the value, similar to the shorthand initializer.
var isSuperSaiyan = true
var city = "Tokyo"
Go automatically determines that isSuperSaiyan is a bool and city is a string based on the values provided.
// Shorthand (:=) - most common, inside functions
name := "Vegeta"
// var with type - when you need a specific type
var score uint = 100
// var without type - similar to shorthand, but can be used at package level
var isActive = false
The shorthand initializer is the most idiomatic way to declare variables in Go when you're inside a function and have an initial value.
Variables are the main approach to storing data in Go, and they can be created in different ways.
For this challenge, create three variables using different declaration methods:
name - A string: Set to the value "Goku" using the shorthand initializer (:=)powerLevel - A uint: Set to the value 9001 using var with a type annotationisSuperSaiyan - A bool: Set to the value true using var with type inference (no type annotation needed)Once created, print all three variables to verify they're set correctly.