Created
October 5, 2015 04:51
-
-
Save IndianGuru/8de60a515310c69e0945 to your computer and use it in GitHub Desktop.
currencylayer.go
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" | |
| "fmt" | |
| "log" | |
| "net/http" | |
| ) | |
| type Currencylayer struct { | |
| Success bool `json:"success"` | |
| Terms string `json:"terms"` | |
| Privacy string `json:"privacy"` | |
| Timestamp int `json:"timestamp"` | |
| Source string `json:"source"` | |
| Quotes struct { | |
| Usdeur float64 `json:"USDEUR"` | |
| Usdinr float64 `json:"USDINR"` | |
| } `json:"quotes"` | |
| } | |
| func main() { | |
| url := fmt.Sprintf("http://apilayer.net/api/live?access_key=YOUR_ACCESS_KEY¤cies=EUR,INR&source=USD") | |
| // Build the request | |
| req, err := http.NewRequest("GET", url, nil) | |
| if err != nil { | |
| log.Fatal("NewRequest: ", err) | |
| return | |
| } | |
| // For control over HTTP client headers, | |
| // redirect policy, and other settings, | |
| // create a Client | |
| // A Client is an HTTP client | |
| client := &http.Client{} | |
| // Send the request via a client | |
| // Do sends an HTTP request and | |
| // returns an HTTP response | |
| resp, err := client.Do(req) | |
| if err != nil { | |
| log.Fatal("Do: ", err) | |
| return | |
| } | |
| // Callers should close resp.Body | |
| // when done reading from it | |
| // Defer the closing of the body | |
| defer resp.Body.Close() | |
| // Fill the record with the data from the JSON | |
| var record Currencylayer | |
| // Use json.Decode for reading streams of JSON data | |
| if err := json.NewDecoder(resp.Body).Decode(&record); err != nil { | |
| log.Println(err) | |
| } | |
| // We want to know what 100 Indian Rupee (INR) is worth in Euros (EUR). | |
| // The formula is USDEUR/USDINR*100 | |
| fmt.Println("Rs. 100 is worth in Euros = ", (record.Quotes.Usdeur/record.Quotes.Usdinr*100.0)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment