go - Trying to create slice of type MovingAvarage -


i using robinus2/golang-moving-average library compute moving avarages, unable assembe slice of these avarages compute ma multiple variables.

ma := []movingaverage.movingaverage{}     ma[0] = movingaverage.new(15)     ma[0].add(3.14) 

what wrong? index out of range error. thanks!

you need either pre-size slice with

ma := make(movingaverage.movingaverage, 5) 

which gives slice of capacity 5 , length 5 each entry set 0 value

better though initialise did add new entries with

ma = append(ma, movingaverage.new(15))  

if know how big eventual slice can pre-allocate underlying array

ma := make(movingaverage.movingaverage, 0, 5) 

which give slice of length 0 capacity 5 don't have repeated memory allocations , moves


Comments

Popular posts from this blog

angular - DownloadURL return null in below code -

meteor - inserting data to database gives error "insert failed: Method '/texts/insert' not found" -