Last modified: Jan 22, 2026 By Alexander Williams
Declare Multiple Variables in One Line Go
Go, or Golang, is known for its simplicity and readability. A common task is declaring variables. You can declare them one by one. But Go offers a more concise way. You can declare multiple variables on a single line. This makes your code cleaner and more efficient.
This guide explains how to do it. We will cover the basic syntax, different initialization methods, and best practices. You will also see practical code examples. This is useful for beginners and experienced developers alike.
Basic Syntax for Multiple Variable Declaration
The standard way uses the var keyword. You list the variable names, followed by their type. The variables are separated by commas. This declares them but does not assign values.
// Declaring three integer variables on one line
var width, height, depth int
After this declaration, width, height, and depth exist. They are of type int. They hold the zero value for integers, which is 0. For more on zero values, see our guide on Declare Variables Without Value in Go (Golang).
Declaring and Initializing on One Line
You can declare and assign values simultaneously. This is often more useful. Use the var keyword with an equals sign. Each variable gets its corresponding value.
// Declaring and initializing three variables
var x, y, z int = 10, 20, 30
Here, x is 10, y is 20, and z is 30. The number of values must match the number of variables. The order matters. The first value goes to the first variable, and so on.
Using Type Inference with :=
Inside functions, you can use the short declaration operator :=. It infers the type from the provided values. This is a very common and idiomatic Go pattern.
package main
import "fmt"
func main() {
// Short declaration for multiple variables
name, age, isProgrammer := "Alice", 30, true
fmt.Println("Name:", name)
fmt.Println("Age:", age)
fmt.Println("Is a programmer?", isProgrammer)
}
Name: Alice
Age: 30
Is a programmer? true
The compiler determines the types automatically. name is a string, age is an int, and isProgrammer is a bool. Remember, := can only be used inside function bodies.
Mixing Variable Types in One Line
You can declare variables of different types on one line. Use the var keyword with parentheses. This is called a "var block" but can be written on a single line.
var (
product string = "Laptop"
price float64 = 999.99
inStock bool = true
)
While this spans multiple lines for readability, the declaration block starts on one line. It's perfect for grouping related variables of different types. For organizing more complex data, you might use a Go List of Strings or other data structures.
Swapping Variables Easily
A clever use of multiple variable declaration is swapping values. You can swap two variables without a temporary variable. Do it all in one line.
package main
import "fmt"
func main() {
a, b := 5, 10
fmt.Println("Before swap: a =", a, ", b =", b)
// Swap the values in one line
a, b = b, a
fmt.Println("After swap: a =", a, ", b =", b)
}
Before swap: a = 5 , b = 10
After swap: a = 10 , b = 5
This works because Go evaluates the right-hand side (b, a) completely before assigning to the left-hand side (a, b). It's a neat and efficient trick.
Returning Multiple Values from Functions
Go functions can return multiple values. Declaring multiple variables on one line is perfect for capturing these returns.
package main
import "fmt"
// A function that returns two values
func calculate(n int) (int, int) {
double := n * 2
square := n * n
return double, square
}
func main() {
// Capture both return values in one line
d, s := calculate(5)
fmt.Printf("Double: %d, Square: %d\n", d, s)
}
Double: 10, Square: 25
The function calculate returns two integers. The line d, s := calculate(5) declares and initializes both d and s in one clean statement. To display results like this, learn different Print Variable Values in Go methods.
Important Considerations and Best Practices
Readability is Key. While declaring many variables on one line saves space, don't overdo it. If you have more than 3 or 4 related variables, it might be okay. For many unrelated variables, use separate lines. This keeps your code easy to read and maintain.
Follow Naming Rules. Always use clear and meaningful names for your variables. This is crucial when they are declared together. Good names make the code self-documenting. Review the Go Variable Naming Rules for Beginners to ensure clarity.
Mind the Scope. Variables declared with := are local to the function. Variables declared with var at the package level are global. Choose the appropriate method based on where you need the variable.
Common Pitfalls to Avoid
Mismatched Counts: Ensure the number of variables matches the number of initial values. A mismatch causes a compile-time error.
// This will cause an error
var a, b, c int = 1, 2
// Error: assignment mismatch: 3 variables but 2 values
Unused Variables: Go does not allow unused variables. If you declare a variable, you must use it. This applies to multi-variable declarations as well. If one variable in the line is unused, the code will not compile.
Conclusion
Declaring multiple variables on one line in Go is a powerful syntactic feature. It promotes concise and clean code. You can use it with the var keyword or the short declaration operator :=.
It is ideal for initializing related variables, swapping values, and handling multi-return functions. Always prioritize code readability. Use this feature wisely to enhance your Go programs without creating confusion.
Mastering this simple technique is a step toward writing idiomatic and efficient Go code. Combine it with other fundamental practices to build robust applications.