func main() { var unitType string = os.Args[1] for _, arg := range os.Args[2:] { v, err := strconv.ParseFloat(arg, 64) if err != nil { fmt.Fprintf(os.Stderr, "cf: %v\n", err) os.Exit(1) } switch unitType { case "l": fallthrough case "len": fallthrough case "length": m := lenconv.Meter(v) in := lenconv.Inch(v) fmt.Printf("%s = %s, %s = %s\n", m, lenconv.MToIn(m), in, lenconv.InToM(in)) case "t": fallthrough case "temp": fallthrough case "temperature": f := tempconv.Fahrenheit(v) c := tempconv.Celsius(v) fmt.Printf("%s = %s, %s = %s\n", f, tempconv.FToC(f), c, tempconv.CToF(c)) } } }
func main() { fmt.Printf("Brrrr! %v\n", tempconv.AbsoluteZeroC) // "Brrrr! -273.15°C" fmt.Println(tempconv.CToF(tempconv.BoilingC)) // "212°F" //練習 2.1: 向tempconv包添加類型、常量和函數用來處理Kelvin絶對溫度的轉換,Kelvin 絶對零度是−273.15°C,Kelvin絶對溫度1K和攝氏度1°C的單位間隔是一樣的。 }
func main() { for _, arg := range os.Args[1:] { t, err := strconv.Parse - Float(arg, 64) if err != nil { fmt.Fprintf(os.Stderr, "cf: %v\n", err) os.Exit(1) } f := tempconv.Fahrenheit(t) c := tempconv.Celsius(t) fmt.Printf("%s = %s, %s = %s\n", f, tempconv.FToC(f), c, tempconv.CToF(c)) } }