Created
June 22, 2015 11:45
-
-
Save jfrobbins/ce7db66881fdb1d7c130 to your computer and use it in GitHub Desktop.
just some practice with syntax, playing with go. This converts Farenheit<->Celsius.
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
| //File tConv.go | |
| //================================================= | |
| //syntax: | |
| // ./tConv -F 96 | |
| // Output: 96.00°F is equal to 35.56°C | |
| //./tConv -C 35.5 | |
| // Output: 35.50°C is equal to 95.90°F | |
| //================================================= | |
| package main | |
| import ( | |
| "fmt" | |
| "flag" | |
| "strconv" | |
| ) | |
| func FtoS(input_num float64) string { | |
| // to convert a float number to a string | |
| return strconv.FormatFloat(input_num, 'f', 2, 64) | |
| } | |
| type myTemp struct { | |
| Temp float64 | |
| Unit string | |
| } | |
| const degree = "°" | |
| const degF = "°F" | |
| const degC = "°C" | |
| func main() { | |
| var isCtemp = flag.Bool("C", false, "Is Celsius temp to converting to Farenheit") | |
| var isFtemp = flag.Bool("F", true, "Is Farenheit temp to converting to Celsius") | |
| var args []string | |
| var in myTemp | |
| var out myTemp | |
| var err error | |
| flag.Parse() | |
| args = flag.Args() | |
| if len(args) == 1 { | |
| in.Temp, err = strconv.ParseFloat(args[0], 64) | |
| if err != nil { | |
| fmt.Println("temp wasn't the first entry after the flags, or was not a number") | |
| return | |
| } | |
| } | |
| if *isCtemp == true { | |
| //°F = °C x 9/5 + 32 | |
| out.Temp = (in.Temp * (9.0/5.0)) + 32.0 | |
| out.Unit = degF | |
| in.Unit = degC | |
| } else if *isFtemp == true { | |
| //°C = (°F - 32) x 5/9 | |
| out.Temp = (in.Temp - 32.0) * (5.0/9.0) | |
| out.Unit = degC | |
| in.Unit = degF | |
| } else { | |
| fmt.Println("No input temp was specified (-F or -C flags)") | |
| return | |
| } | |
| fmt.Println(FtoS(in.Temp) + in.Unit + " is equal to " + FtoS(out.Temp) + out.Unit + "\n") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment