Last active
May 26, 2019 07:07
-
-
Save utx0/93994569b25d56f6a883e164881068f1 to your computer and use it in GitHub Desktop.
golang Binance API example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
// exchangeInfoData is the primary json object | |
type exchangeInfoData struct { | |
Symbols []symbolInfoData `json:"symbols"` | |
} | |
// symbolInfoData is a contained object within the exchangeInfoData json object | |
// there are many other possible objects within this json payload however only this one will be parsed/Unmarshal into a go struct | |
type symbolInfoData struct { | |
Symbol string `json:"Symbol"` | |
} | |
const ( | |
restUrl = "https://api.binance.com" | |
exchangeInfo = "/api/v1/exchangeInfo" | |
) | |
func main() { | |
resp, err := http.Get(restUrl + exchangeInfo) | |
if err != nil { | |
log.Panic("Error: ", err.Error()) | |
} | |
// ReadAll returns a []byte | |
b, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Panicf("Error: %v", err.Error()) | |
} | |
// output raw resp.Body | |
log.Println("b: ",string(b)) | |
// init a nil struct | |
// var data = exchangeInfoData // Can be done this way too. | |
data := exchangeInfoData{} | |
// Unmarshal/Parse the raw json into your struct data type | |
jsonErr := json.Unmarshal(b, &data) | |
if jsonErr != nil { | |
log.Panicf("Error: %v", jsonErr) | |
} | |
// Loop over the data and output all the good stuff. | |
for _, s := range data.Symbols { | |
log.Println("symbol: ", s.Symbol) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment