Declaring Multiple Variables in Go
In Go, it is possible to declare multiple variables at once, allowing for efficient and concise code.
Question:
Can you declar multiple variables simultaneously in Go, similar to Python where you can assign a value to multiple variables using = operator?
Answer:
Yes, Go supports declaring multiple variables at once using the var keyword, followed by a list of variable names, each separated by a comma.
var a, b, c string a = "foo" fmt.Println(a)
In this example, three variables (a, b, and c) of type string are declared at once. The a variable is then assigned the value "foo".
Inline Assignment:
Although not as convenient as in Python, Go allows for a similar inline assignment syntax, using the := operator.
a, b, c := 80, 80, 80
In this case, three variables (a, b, and c) of type int are declared and assigned the value 80 simultaneously.
The above is the detailed content of Can Go Declare Multiple Variables Simultaneously?. For more information, please follow other related articles on the PHP Chinese website!