Skip to content

Instantly share code, notes, and snippets.

@yitsushi
Created May 19, 2015 13:28
Show Gist options
  • Save yitsushi/8ebc63ed2e36ea484b31 to your computer and use it in GitHub Desktop.
Save yitsushi/8ebc63ed2e36ea484b31 to your computer and use it in GitHub Desktop.
Coinbase API (EUR<->BTC) sell/buy value
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
)
type Currency struct {
Name string
Value float64
}
func main() {
currencies := make(map[string]*Currency)
url := "https://api.coinbase.com/v1/currencies/exchange_rates"
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
content, _ := ioutil.ReadAll(resp.Body)
decorator := json.NewDecoder(strings.NewReader(string(content)))
for {
var v map[string]string
if err := decorator.Decode(&v); err != nil {
break
}
for k, c := range v {
value, _ := strconv.ParseFloat(c, 10)
currencies[k] = &Currency{Name: k, Value: value}
}
}
log.Printf("BUY BTC: %f EUR\n", 1.0/currencies["eur_to_btc"].Value)
log.Printf("SELL BTC: %f EUR\n", currencies["btc_to_eur"].Value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment