November 2017
Intermediate to advanced
670 pages
17h 35m
English
AggregateT iterates over a slice, aggregating each element into a single result. AggregateT is comparable to LINQ's Aggregate and underscores reduce function.
Here is the signature:
func (ExampleSlice) AggregateT(func(T, Example) T) T
In the following example, we specify in our comment annotation that we want gen to create an Aggregate function that operates over a slice of strings. We define a join function that we pass to AggregateString, which performs the join operation:
// +gen slice:"Aggregate[string]" type Employee struct{ Name string Department string } employees := EmployeeSlice { {"Alice", "Accounting"}, {"Bob", "Back Office"}, {"Carly", "Containers"}, } join := func(state string, e Employee) string { if state != ...