I've stumbled upon this repository,
The for loop has multiple brackets:
for _, test := range []struct {
missingField string
errorMessage string
}{
{"Name", "missing name for receiver"},
(...)
} {
fields := removeFromStrSlice(mandatory, test.missingField)
(...)
}
configErrorTestRunner(t, config, test.errorMessage)
}
I haven't been able to find anything about this in the go documentation, what is this construct?
The first bracket pair is part of the struct type definition which has the syntax:
struct{
field1 type1
field2 type2
...
}
The second pair is part of the composite literal value, creating a value for the slice of this struct. It has the syntax of:
[]elementType{value1, value2, ...}
The inner, embedded brackets are part of the composite literals creating the struct values of the slice. The type is omitted (it's known from the slice type), so each {field1Value, fieldValue2...}
is a struct value.
The third pair defines the block of the for
statement. The for
statement iterates over the elements of the slice defined by the mentioned composite literal.