From 0e5b3a9dc2c18b2c5ae63c7cef9655c6ad7045a8 Mon Sep 17 00:00:00 2001 From: Meutel Date: Sun, 9 Jul 2017 10:28:56 +0200 Subject: [PATCH] package --- .gitignore | 1 + converter-main/main.go | 16 ++++++++++++++++ converter/{main.go => converter.go} | 16 ++++------------ 3 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 converter-main/main.go rename converter/{main.go => converter.go} (84%) diff --git a/.gitignore b/.gitignore index a196bf1..499febb 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ datediff/datediff example-json/example-json financialjson/financialjson statecsv2json/statecsv2json +converter-main/converter-main diff --git a/converter-main/main.go b/converter-main/main.go new file mode 100644 index 0000000..1535238 --- /dev/null +++ b/converter-main/main.go @@ -0,0 +1,16 @@ +package main + +import "fmt" +import "log" +import "os" +import "meutel.net/meutel/go-examples/converter" + +func main() { + if len(os.Args) < 3 { + log.Fatalln("Needs 2 arguments") + } + input := os.Args[1] + convTo := os.Args[2] + + fmt.Println(converter.Convert(input, convTo)) +} diff --git a/converter/main.go b/converter/converter.go similarity index 84% rename from converter/main.go rename to converter/converter.go index 6c2390b..dc51f27 100644 --- a/converter/main.go +++ b/converter/converter.go @@ -1,8 +1,7 @@ -package main +package converter import "fmt" import "log" -import "os" import "strconv" import "strings" @@ -23,13 +22,7 @@ var ( convVal float64 ) -func main() { - if len(os.Args) < 3 { - log.Fatalln("Needs 2 arguments") - } - input := os.Args[1] - convTo := os.Args[2] - +func Convert(input, convTo string) string { switch { case strings.HasSuffix(input, markerKm): inUnit = markerKm @@ -58,8 +51,6 @@ func main() { asMeter = inVal } - // fmt.Println(inVal, inUnit, asMeter, convTo) - switch convTo { case markerMiles: convVal = asMeter / (MilesToKm * 1000) @@ -72,5 +63,6 @@ func main() { default: log.Fatalln("Invalid unit: " + convTo) } - fmt.Printf("%.2f%s\n", convVal, convTo) + + return fmt.Sprintf("%.2f%s", convVal, convTo) }