Golang : Temperatures conversion example
Writing this down for my own future reference. Need these temperature conversions for developing an agriculture IoT application. This simple tutorial covers how to calculate and convert temperatures in Celsius, Fahrenheit and Kelvin. The temperature information will be displayed on a localized map and allow the users(farmers) to plan their daily tasks.
Here you go!
package main
import "fmt"
type (
// or centigrade
Celsius float64
Kelvin float64
Fahrenheit float64
)
const (
// standard stuff from Physics
// useful for triggering alarms or sequence of actions
// to adjust temperature
WaterBoilingPoint Celsius = 100
WaterFreezingPoint Celsius = 0
AbsoluteZero Celsius = -273.15
)
func Celsius2Fahrenheit(c Celsius) Fahrenheit {
return Fahrenheit(c*9/5 + 32)
}
func Celsius2Kelvin(c Celsius) Kelvin {
return Kelvin(c + 273.15)
}
func Kelvin2Celsius(k Kelvin) Celsius {
return Celsius(k - 273.15)
}
func Fahrenheit2Celsius(f Fahrenheit) Celsius {
return Celsius((f - 32) * 5 / 9)
}
func main() {
// 30 degree Celsius to Fahrenheit
fmt.Println("30 degree Celsius to Fahrenheit is : ", Celsius2Fahrenheit(30), " Fahrenheit.")
// 89 degree Fahrenheit to Celsius
fmt.Println("89 degree Fahrenheit to Celsius is : ", Fahrenheit2Celsius(89), " Celsius.")
fmt.Printf("The water boiling point in Celsius is %v or %v Fahrenheit or %v Kelvin.\n\n",
WaterBoilingPoint, Celsius2Fahrenheit(WaterBoilingPoint), Celsius2Kelvin(WaterBoilingPoint))
}
Output:
30 degree Celsius to Fahrenheit is : 86 Fahrenheit.
89 degree Fahrenheit to Celsius is : 31.666666666666668 Celsius.
The water boiling point in Celsius is 100 or 212 Fahrenheit or 373.15 Kelvin.
References:
See also : Golang : Find location by IP address and display with Google Map
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.4k Golang : Find the longest line of text example
+7.6k Swift : Convert (cast) String to Double
+29.2k Golang : Login(Authenticate) with Facebook example
+10k Golang : cannot assign type int to value (type uint8) in range error
+5.7k nginx : force all pages to be SSL
+11.3k SSL : The certificate is not trusted because no issuer chain was provided
+23.9k Golang : How to validate URL the right way
+11.1k Golang : Concatenate (combine) buffer data example
+8.1k Your page has meta tags in the body instead of the head
+12.5k Golang : Convert int(year) to time.Time type
+20.7k Golang : How to force compile or remove object files first before rebuild?
+5.9k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?