Welcome to Go!
There's a couple things going on here, which you can break down into two overarching parts.
Part 1, var/type definition:
var testCases = []struct {
description string
planet Planet
seconds float64
expected float64
}
This says the following things:
- Create a new variable, called testCases
- testCases will contain a slice of (anonymous) structs ( []struct {.....} )
- and each of those structs will contain 4 fields: description, planet, seconds, expected
Part 2, the instantiation/struct literal:
{
{
description: "age on Earth",
planet: "Earth",
seconds: 1000000000,
expected: 31.69,
},
{
description: "age on Mercury",
planet: "Mercury",
seconds: 2134835688,
expected: 280.88,
}, - Instantiate this slice of structs right in-line (this is called a literal)
- Repeated anonymous struct literals for each item in the slice
The likely purpose of this code, overall, is so that later on down, something can iterate `testCases` and get a number of `testCase`, each with test expected values.
You could also write this code like:
type TestCase struct {
description string
planet Planet
seconds float64
expected float64
}
var testCases []TestCase
testCases = append(testCases, TestCase{
description: "age on Uranus",
planet: "Uranus",
seconds: 3210123456,
expected: 1.21,
})
[.....etc....]
with an append call for each case you want to add.
The example you provided just streamlines the whole process, and takes advantage of slice literals, anonymous struct definitions, and implicit struct literals within a slice definition.
Hope that helps!
Brian