November 2017
Intermediate to advanced
670 pages
17h 35m
English
We'll test our new empty interface-based Map function by defining a transformation function. This function will multiply every item in the collection by 10:
func main() { transformation10 := func(curVal, _, _ Object) Object { return curVal.(int) * 10 } result := Map(Collection{1, 2, 3, 4}, transformation10) fmt.Printf("result: %vn", result)
We pass a collection of the numbers 1, 2, 3, and 4 as well as the transformation function.
The output will be as follows:
result: [10 20 30 40]
Now, let's pass our Map function a collection of strings:
transformationUpper := func(curVal, _, _ Object) Object { return strings.ToUpper(curVal.(string)) } result = Map(Collection{"alice", "bob", "cindy"}, transformationUpper) ...