From the course: Advanced Go Programming: Data Structures, Code Architecture, and Testing
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Solution: Rolling mean - Go Tutorial
From the course: Advanced Go Programming: Data Structures, Code Architecture, and Testing
Solution: Rolling mean
(upbeat music) - [Instructor] Let's have a look at my implementation of the rolling mean method on lines 50 to 73. Channel and slice management are the key to solving this problem correctly. First, we declare a for loop which will allow our function to keep reading values until instructed to stop. On lines 52 to 57, we read the value and corresponding ok value from the input channel. If the ok value is false, indicating that the input channel has been closed, then we close the output channel and return terminating our infinite loop. Next up on lines 59 to 61, save the value received from the input channel, then increment the position and use the modular operator to ensure that we never exceed the capacity of the window. We increment the position after the element has been added, allowing us to use the index zero on the first element. On line 63 to 67, check the window filled field to see if we have enough values…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.