I am using the RobinUS2/golang-moving-average library to compute moving avarages, but I am unable to assembe a slice of these avarages to compute MA for multiple variables.
ma := []movingaverage.MovingAverage{}
ma[0] = movingaverage.New(15)
ma[0].Add(3.14)
What could be wrong? I get an index out of range error. Thanks!
You need to either pre-size the slice with
ma := make(movingaverage.MovingAverage, 5)
Which gives a slice of capacity 5 and length 5 with each entry set to the zero value
Better though to initialise it as you did but then to add new entries with
ma = append(ma, movingaverage.New(15))
If you know how big your eventual slice will be you can pre-allocate the underlying array with
ma := make(movingaverage.MovingAverage, 0, 5)
which will give you a slice of length 0 but capacity 5 so you don't have to do repeated memory allocations and moves