How to Declare Multiple variables in Golang with examples
Some programming languages allow you to declare multiple variables with a single line.
This post shows you how to declare multiple variables.
In Golang, Variables are declared as follows
var variable type
How to declare multiple variables of the same type in Golang?
Is it possible to declare multiple variables in Golang?
Yes, It is possible to declare multiple variables of the same type in a single statement.
Here is a declaration syntax for multiple variables.
var v1, v1, v3 string;
Three variables are declared v1,v2, and v3 with string type.
How to declare multiple variables of a different type in Golang?
And It gives an error to declare multiple variables of different types with the type declaration.
You can also declare and assign variables
Here is code to declare and assign multiple variables.
v1, v2 := "one", "two";
The above type is removed because of the initial value.
You can declare and assign multipe variables of different types.
var num, str = 12, "one"
fmt.Println(num, str) // 12 one
Conclusion
In golang, You can declare multiple variables of the same type, does not accept multiple variables of different types with the type declaration.
Still, you can declare multiple variables of different types with an initial value, without type.