From the course: Go Practice: Functions

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Solution: Function composition

Solution: Function composition - Go Tutorial

From the course: Go Practice: Functions

Solution: Function composition

- [Instructor] Let's talk about the implemented CalculateVariance function based on the structure we've already discussed. We'll start with the error case. On line 18, I use an IF statement to check whether the number slice is nil or empty. In this case, it will return nil for the result and return an error. On line 21, I declare the variable n which will hold the number of elements as a float64 type. This will make it easier for us to calculate the mean and variance without having to cast the length to float64 multiple times. As you remember from challenge one, the float64 type is required to calculate decimal point numbers. Next, I'll implement our sum inside the scope of the CalculateVariance function. This function takes in a function f as a parameter and returns a float64. The function f itself takes in a single int parameter and returns a float64. The function parameter makes it easier for us to calculate sums…

Contents