This commit is contained in:
Meutel 2017-07-09 10:28:56 +02:00
parent 6003765d39
commit 0e5b3a9dc2
3 changed files with 21 additions and 12 deletions

1
.gitignore vendored
View File

@ -43,3 +43,4 @@ datediff/datediff
example-json/example-json example-json/example-json
financialjson/financialjson financialjson/financialjson
statecsv2json/statecsv2json statecsv2json/statecsv2json
converter-main/converter-main

16
converter-main/main.go Normal file
View File

@ -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))
}

View File

@ -1,8 +1,7 @@
package main package converter
import "fmt" import "fmt"
import "log" import "log"
import "os"
import "strconv" import "strconv"
import "strings" import "strings"
@ -23,13 +22,7 @@ var (
convVal float64 convVal float64
) )
func main() { func Convert(input, convTo string) string {
if len(os.Args) < 3 {
log.Fatalln("Needs 2 arguments")
}
input := os.Args[1]
convTo := os.Args[2]
switch { switch {
case strings.HasSuffix(input, markerKm): case strings.HasSuffix(input, markerKm):
inUnit = markerKm inUnit = markerKm
@ -58,8 +51,6 @@ func main() {
asMeter = inVal asMeter = inVal
} }
// fmt.Println(inVal, inUnit, asMeter, convTo)
switch convTo { switch convTo {
case markerMiles: case markerMiles:
convVal = asMeter / (MilesToKm * 1000) convVal = asMeter / (MilesToKm * 1000)
@ -72,5 +63,6 @@ func main() {
default: default:
log.Fatalln("Invalid unit: " + convTo) log.Fatalln("Invalid unit: " + convTo)
} }
fmt.Printf("%.2f%s\n", convVal, convTo)
return fmt.Sprintf("%.2f%s", convVal, convTo)
} }