Declare multiple Go variables on one line using comma-separated names or a block declaration for mixed types.
Declare multiple variables of the same type on a single line by listing their names separated by commas before the type.
var x, y, z int
For variables with different types or initial values, use a block declaration with parentheses.
var (
name string
age int
done bool
)
You can also declare and initialize them simultaneously.
x, y, z := 1, 2, 3
Or with explicit types and values:
var (
name string = "Alice"
age int = 30
)
Declaring multiple variables in Go lets you create several storage containers for your data in one go instead of writing a separate line for each one. It keeps your code shorter and easier to read when you need a group of related items. Think of it like listing multiple ingredients on a single grocery list line rather than writing a new line for every single item.