currency/main.go
Michael 87d9a931a3 first commit
Signed-off-by: Michael <michael.lindman@gmail.com>
2021-10-27 02:29:00 +01:00

42 lines
766 B
Go

package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"git.0cd.xyz/michael/toolbox/req"
)
const url = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/"
func main() {
from, to, value := ui()
body, _, err := req.Get(url+from+"/"+to+".json", nil)
if err != nil {
log.Fatal(err)
}
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
log.Fatal(err)
}
v, ok := result[to].(float64)
if !ok {
log.Fatal("invalid conversion")
}
fmt.Printf("%.2f %s\n", v*value, to)
}
func ui() (from, to string, value float64) {
if len(os.Args) < 4 {
os.Exit(1)
}
val, err := strconv.ParseFloat(os.Args[3], 64)
if err != nil {
log.Fatal(err)
}
return os.Args[1], os.Args[2], val
}