What is this parenthesis enclosed variable declaration syntax in Go?
var (...)
(and const (...)
are just shorthand that let you avoid repeating the var
keyword. It doesn't make a lot of sense with a single variable like this, but if you have multiple variables it can look nicer to group them this way.
It doesn't have anything to do with exporting. Variables declared in this way are exported (or not) based on the capitalization of their name, just like variables declared without the parentheses.
This code
// What's this syntax ? Is it exported ?
var (
rootDir = path.Join(home(), ".coolconfig")
)
is just a longer way of writing
var rootDir = path.Join(home(), ".coolconfig")
However it is useful when declaring lots of vars at once. Instead of
var one string
var two string
var three string
You can write
var (
one string
two string
three string
)
The same trick works with const
and type
too.