Created
May 19, 2015 13:28
-
-
Save yitsushi/8ebc63ed2e36ea484b31 to your computer and use it in GitHub Desktop.
Coinbase API (EUR<->BTC) sell/buy value
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" | |
"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