Go Language Program to Convert Fahrenheit to Celsius


Temperature conversions are crucial in many scientific expeditions, Fahrenheit is a scale of temperature and celsius is also a temperature scale, but sometimes we need to convert fahrenheit to celsius for medical settings, travels, and more. In this article, we are going to explore conversion of Fahrenheit temperature to corresponding Celsius in Go programming language.

Explanation

The basic idea can be distilled into the given formula. Here, °C represents value in Celsius and °F represents value in Fahrenheit.

°C = (°F - 32) * 5/9

This is the formula used to convert fahrenheit to celsius.

Syntax

func fahrenheitToCelsius(fahrenheit float64) float64

The syntax defines a function `fahrenheitToCelsius` which converts temperature value in Fahrenheit, provided as `float64`, to equivalent value in Celsius, applying a simple formula to perform the conversion and returning the resulting Celsius temperature as `float64`.

func fahrenheitToCelsiusUsingStrconv(fahrenheitStr string) (float64, error)

The syntax represents a method called `fahrenheitToCelsiusUsingStrconv` designed to convert a temperature value provided as a string in Fahrenheit to its equivalent value in Celsius. It returns the converted temperature as a `float64` and also includes an `error` handling mechanism to manage any potential conversion errors. This function is useful when dealing with string inputs, offering greater flexibility and error control in the temperature conversion process.

Algorithm

  • First, choose Fahrenheit (°F) as the temperature unit.

  • Take the Fahrenheit temperature in degrees (°F), subtract 32, and then multiply by 5/9.

  • Use Step 2's math to get the temperature in Celsius (°C).

Example 1

In this example, we use direct formula to Convert Fahrenheit to Celsius in go: (°F - 32) * 5/9 = °C. fahrenheitToCelsius function converts value from Fahrenheit to Celsius.

package main
import (
	"fmt"
)
func fahrenheitToCelsius(fahrenheit float64) float64 {
	celsius := (fahrenheit - 32) * 5 / 9
	return celsius
}
func main() {
	result := fahrenheitToCelsius(68)
	fmt.Println("Method 1 - 68° Fahrenheit to Celsius:", result, "°C")
}

Output

Method 1 - 68° Fahrenheit to Celsius: 20 °C

Example 2

In this example, Convert Fahrenheit to Celsius in go from string to float64 using strconv.ParseFloat built-in function. fahrenheitToCelsius uses direct formula to instantly convert Fahrenheit value to Celsius. The fahrenheitToCelsiusUsingStrconv function takes input Fahrenheit number in string form, converts it to float64 using strconv.ParseFloat, and then calls fahrenheitToCelsius method.

package main
import (
	"fmt"
	"strconv"
)
func fahrenheitToCelsius(fahrenheit float64) float64 {
	celsius := (fahrenheit - 32) * 5 / 9
	return celsius
}
func fahrenheitToCelsiusUsingStrconv(fahrenheitStr string) (float64, error) {
	fahrenheit, err := strconv.ParseFloat(fahrenheitStr, 64)
	if err != nil {
      	return 0, err
    }
    celsius := fahrenheitToCelsius(fahrenheit)
    return celsius, nil
}
func main() {
	// Method 2
	result, err := fahrenheitToCelsiusUsingStrconv("73.5")
	if err != nil {
       	fmt.Println("Error:", err)
    } else {
    	fmt.Println("Method 2 - 73.5° Fahrenheit to Celsius:", result, "°C")
    }
}

Output

Method 2 - 73.5° Fahrenheit to Celsius: 23.055555555555557 °C

Real Life Implementation

  • Travel and Weather: Tourists from countries that use the Fahrenheit scale may need to convert weather forecasts or temperature readings while visiting countries that use the Celsius system. This conversion helps to improve knowledge of the local climate conditions.

  • Medical Settings: Temperature values on the Fahrenheit scale are often expressed in several medical situations, notably when referring to body temperature. Conversions of these measurements are essential for healthcare practitioners who use the Celsius scale in order to preserve consistency in their records and enable analysis.

Conclusion

Fahrenheit is a temperature scale that is used in the United states and few other countries while celsius is used in europe fr every day weather reporting. In this article, we explored two methods to Convert Fahrenheit to Celsius in go. The first method involves a simple formula, while the second method utilises the `strconv` package to handle string inputs. Overall, these methods provide flexibility and ease of use for temperature conversions.

Updated on: 18-Oct-2023

78 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements