Golang struct array growing in loop

By user2777145

Can someone please tell me why msg array is growing? I cannot figure out what is going on here. Or what I seem to be missing!

If I understand go right msg.RateQuote = msg.RateQuote[0:] should drop off the last input and keep the current input.

msg array is growing in size. Why?

How would I stop this from going in size and release the last slice back to the system ram?

package main
import (
        "log"
        "fmt"
        "net/http"
        "bytes"
        "io/ioutil"
        "strings"
        "strconv"
        "github.com/pquerna/ffjson/ffjson"
)
type MsgRatesArray struct {
        RateQuote []MsgRateQuoteJson `json:"Rates"`
}
type DBRatesArray struct {
        RateQuote []DBTick
}
type MsgRateQuoteJson struct {
        SymbolName   string  `json:"Symbol"`
        SymbolBid    float64 `json:"Bid"`
        SymbolAsk    float64 `json:"Ask"`
        SymbolSpread float64 `json:"Spread"`
        SymbolPT     int64   `json:"ProductType"`
        TickMid      float64
}
type DBTick struct {
        SymbolName   string
        SymbolBid    float64
        SymbolAsk    float64
        SymbolSpread float64
        SymbolPT     int64
        TickMid      float64
}
var respBytes []byte
var tick DBRatesArray
var msg MsgRatesArray
func main() {
  //
  // Test for why msg array grows in size???
  //
  for iii := 1;  iii<=9999; iii++ {
    //var msg MsgRatesArray
    //
    //Get Quotes from FXCM with URL of https://ratesjson.fxcm.com/DataDisplayer
    //
    response,err := http.Get("https://ratesjson.fxcm.com/DataDisplayer")
    if err != nil {
        log.Fatal(err)
    }
    //
    // make sure to release responce memory
    //
    defer response.Body.Close()
    //
    // Get bad JSON and fix JSON
    respBytes, err := ioutil.ReadAll(response.Body)
    jsonBytes := respBytes[bytes.Index(respBytes, []byte("{")):bytes.LastIndex(respBytes, []byte("}"))+1]
    jsonString := string(jsonBytes)
    //fmt.Println(jsonString)
    //
    // Use a positive number to indicate max replacement count to fix bad JSON string so we can remove comma in JSON ARRAY.
    //
    result := strings.Replace(jsonString, "",}", ""}", -1)
    //fmt.Println(result)
    //
    // Turn GOOD JSON string back to JSON BYTES (BIN)
    //
    jsonBytes2 := []byte(result)
    //
    // Parse JSON !
    //
    err = ffjson.Unmarshal(jsonBytes2, &msg)
    if err != nil {
        panic(err)
    }
    //
    // Remove last JSON from msg array
    //
    msg.RateQuote = msg.RateQuote[0:]
  }
}
func (msg *MsgRateQuoteJson) UnmarshalJSON(data []byte) (err error) {
    //
    // set local var
    //
    m := map[string]string{}
    var tAsk float64
    var tBid float64
    //
    // Get values out of JSON
    //
    if err = ffjson.Unmarshal(data, &m); err != nil {
        return err
    }
    //
    // Get bid/ask from current JSON
    //
    tAsk, err = strconv.ParseFloat(m["Bid"], 64)
    //
    // GABE error check
    //
    tBid, err = strconv.ParseFloat(m["Bid"], 64)
    //
    // GABE error check
    //
    //
    // Check array to see if we have new values from old array of JSON Quotes
    //
    if ( ( tAsk != msg.SymbolBid ) && ( tBid != msg.SymbolAsk ) && (msg.SymbolAsk!=0 || msg.SymbolBid!=0) ) {
        fmt.Printf("Add to database: %s:%fn", m["Symbol"],(tAsk+tBid/2))
    }
    //
    // Process JSON data and over write the old data in Array
    //
    msg.SymbolName = m["Symbol"]
    if msg.SymbolBid, err = strconv.ParseFloat(m["Bid"], 64); err != nil {
        return err
    }
    if msg.SymbolAsk, err = strconv.ParseFloat(m["Ask"], 64); err != nil {
        return err
    }
    if msg.SymbolSpread, err = strconv.ParseFloat(m["Spread"], 64); err != nil {
        return err
    }
    if msg.SymbolPT, err = strconv.ParseInt(m["ProductType"], 10, 64); err != nil {
        return err
    }
    msg.TickMid = Truncate( ( msg.SymbolBid + msg.SymbolAsk ) / 2 )
    return nil
}
// truncate a float to five levels of precision
func Truncate(some float64) float64 {
        return float64(int(some * 100000)) / 100000
}

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273