Created
October 20, 2020 05:08
-
-
Save it6c65/f2eba59db21306f5bcdf3c2deb5bce6b to your computer and use it in GitHub Desktop.
DolarToday en un binario (Golang)
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" | |
"net/http" | |
"time" | |
s "strings" | |
) | |
var connector = &http.Client{Timeout: 10 * time.Second} | |
var puts = fmt.Println | |
// Datos de la API de DolarToday | |
type DolarToday struct { | |
EUR struct { | |
Cencoex float64 `json:"cencoex"` | |
Dolartoday float64 `json:"dolartoday"` | |
Efectivo float64 `json:"efectivo"` | |
EfectivoCucuta float64 `json:"efectivo_cucuta"` | |
EfectivoReal float64 `json:"efectivo_real"` | |
Promedio float64 `json:"promedio"` | |
PromedioReal float64 `json:"promedio_real"` | |
Sicad1 float64 `json:"sicad1"` | |
Sicad2 float64 `json:"sicad2"` | |
TransferCucuta float64 `json:"transfer_cucuta"` | |
Transferencia float64 `json:"transferencia"` | |
} `json:"EUR"` | |
EURUSD struct { | |
Rate float64 `json:"rate"` | |
} `json:"EURUSD"` | |
MISC struct { | |
Petroleo string `json:"petroleo"` | |
Reservas string `json:"reservas"` | |
} `json:"MISC"` | |
USD struct { | |
BitcoinRef float64 `json:"bitcoin_ref"` | |
Cencoex float64 `json:"cencoex"` | |
Dolartoday float64 `json:"dolartoday"` | |
Efectivo float64 `json:"efectivo"` | |
EfectivoCucuta float64 `json:"efectivo_cucuta"` | |
EfectivoReal float64 `json:"efectivo_real"` | |
LocalbitcoinRef float64 `json:"localbitcoin_ref"` | |
Promedio float64 `json:"promedio"` | |
PromedioReal float64 `json:"promedio_real"` | |
Sicad1 float64 `json:"sicad1"` | |
Sicad2 float64 `json:"sicad2"` | |
TransferCucuta float64 `json:"transfer_cucuta"` | |
Transferencia float64 `json:"transferencia"` | |
} `json:"USD"` | |
USDCOL struct { | |
Rate float64 `json:"rate"` | |
Ratecash float64 `json:"ratecash"` | |
Ratetrm float64 `json:"ratetrm"` | |
Setfxbuy float64 `json:"setfxbuy"` | |
Setfxsell float64 `json:"setfxsell"` | |
Trmfactor float64 `json:"trmfactor"` | |
Trmfactorcash float64 `json:"trmfactorcash"` | |
} `json:"USDCOL"` | |
Timestamp struct { | |
Dia string `json:"dia"` | |
DiaCorta string `json:"dia_corta"` | |
Epoch string `json:"epoch"` | |
Fecha string `json:"fecha"` | |
FechaCorta string `json:"fecha_corta"` | |
FechaCorta2 string `json:"fecha_corta2"` | |
FechaNice string `json:"fecha_nice"` | |
} `json:"_timestamp"` | |
} | |
func getData(url string, target interface{}) error { | |
response, err := connector.Get(url) | |
if err != nil { | |
return err | |
} | |
defer response.Body.Close() | |
decoder := json.NewDecoder(response.Body) | |
err = decoder.Decode(target) | |
if err != nil { | |
fmt.Println(err, err) | |
} | |
return err | |
} | |
func main() { | |
dt := new(DolarToday) | |
getData("https://s3.amazonaws.com/dolartoday/data.json", dt) | |
puts(s.Repeat("*", 17), "DolarToday", s.Repeat("*", 21)) | |
puts("* ","El Dolar está en: ", dt.USD.Promedio, "Bs", s.Repeat(" ", 13), "*") | |
puts("* ","El Euro está en: ", dt.EUR.Promedio, "Bs", s.Repeat(" ", 13), "*") | |
puts("* ","Actualizado en: ", dt.Timestamp.Fecha, " *") | |
puts(s.Repeat("*", 50)) | |
puts("Presiona 'Enter' para continuar...") | |
fmt.Scanln() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment