Multiple variables of different types in one line in Go (without short variable declaration syntax)

Viewed 8930

I was wondering if there's a way with Go to declare and initialise multiple variables of different types in one line without using the short declaration syntax :=.

Declaring for example two variables of the same type is possible:

var a, b string = "hello", "world"

Declaring variables of different types with the := syntax is also possible:

c, d, e := 1, 2, "whatever"

This gives me an error instead:

var f int, g string = 1, "test"

Of course I'd like to keep the type otherwise I can just use the := syntax.

Unfortunately I couldn't find any examples so I'm assuming this is just not possible?

If not, anyone knows if there's a plan to introduce such syntax in future releases?

2 Answers

This isn't exactly specific to the OP's question, but since it gets to appear in search results for declaring multiple vars in a single line (which isn't possible at the moment). A cleaner way for that is:

var (
    n []int
    m string
    v reflect.Value
)
Related