Using Go, I am hoping to query an API endpoint and output the results to a Gorm SQLite DB. This has been done before (here) but I need to write the code myself.
The API endpoint returns a JSON array, and for each trade in the array, I would like to place it into a struct, then add that as row in the SQLite database.
The struct is defined below:
type Trade struct {
TradeID int64 `json:"id"`
Price string `json:"price"`
Qty string `json:"qty"`
QuoteQty string `json:"quoteQty"`
Time int64 `json:"time"`
IsBuyerMaker bool `json:"isBuyerMaker"`
IsBestMatch bool `json:"isBestMatch"`
}
These types may seem weird but was determined by using PowerShell with the below code:
PS C:\Git> $body = @{"symbol" = "ETHEUR";"limit" = 1000}
PS C:\Git> $response = Invoke-RestMethod https://api.binance.com/api/v3/trades -Body $body
PS C:\Git> $response[0] | gm
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
id NoteProperty long id=21731777
isBestMatch NoteProperty bool isBestMatch=True
isBuyerMaker NoteProperty bool isBuyerMaker=True
price NoteProperty string price=3539.03000000
qty NoteProperty string qty=0.28600000
quoteQty NoteProperty string quoteQty=1012.16258000
time NoteProperty long time=1620822731248
So, the Go function I have so far is as follows:
func getTrade(symbol string, limit int) {
uri := fmt.Sprintf("https://api.binance.com/api/v3/trades?symbol=%v&limit=%v", symbol, limit)
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
// Create the fields in the database based on the Trade Struct
db.AutoMigrate(&Trade{})
// Query Binance API endpoint
response, err := http.Get(uri)
// Log if an error occurred
if err != nil {
log.Fatalln(err)
}
// Defer closing object
defer response.Body.Close()
// Create trade struct
trade := Trade{}
From here I have attempted but failed a few various methods of converting the response to a struct. These methods include:
// Unmarshal bytes to data struct
_ = json.Unmarshal([]byte(responseData), &trade)
var myClient = &http.Client{Timeout: 10 * time.Second}
func getJson(url string, target interface{}) error {
response, err := myClient.Get(url)
if err != nil {
return err
}
defer response.Body.Close()
return json.NewDecoder(response.Body).Decode(target)
}
// Iterate through trades returned (this doesn't work)
for _, trade := range responseData {
tradeVariable := Trade{}
tradeVariable.TradeID = trade.id
fmt.Println(tradeVariable.TradeID)
}
I'm feeling really stuck and like I'm missing something to do with marshalling, can someone please help me out?

trades := []Trade{}and pass&tradestojson.Unmarshaland make sure NOT to discard the error with_but instead properly check it and handle it. If there's something wrong with the struct's field types the error returned byjson.Unmarshalwill tell you very clearly.