Introduction
Temperature conversion is a fundamental concept in many scientific and practical applications.
Converting temperatures between different units such as Celsius and Fahrenheit is a routine task in
various fields including weather reporting, engineering, and scientific research.
Objective
The objective of this program is to provide a tool for converting temperatures between Celsius and Fahrenheit.
The user will be prompted to enter the temperature value and the desired conversion type (Celsius to Fahrenheit or Fahrenheit to Celsius).
Program Code
package main
import (
"fmt"
"os"
"strconv"
)
func celsiusToFahrenheit(celsius float64) float64 {
return (celsius * 9 / 5) + 32
}
func fahrenheitToCelsius(fahrenheit float64) float64 {
return (fahrenheit - 32) * 5 / 9
}
func main() {
fmt.Println("Temperature Conversion Program")
fmt.Println("Choose conversion type:")
fmt.Println("1. Celsius to Fahrenheit")
fmt.Println("2. Fahrenheit to Celsius")
var choice int
fmt.Print("Enter your choice (1 or 2): ")
_, err := fmt.Scan(&choice)
if err != nil {
fmt.Println("Invalid input. Please enter a number.")
os.Exit(1)
}
var temp float64
switch choice {
case 1:
fmt.Print("Enter temperature in Celsius: ")
_, err := fmt.Scan(&temp)
if err != nil {
fmt.Println("Invalid input. Please enter a valid number.")
os.Exit(1)
}
fahrenheit := celsiusToFahrenheit(temp)
fmt.Printf("%.2f Celsius is equal to %.2f Fahrenheit\n", temp, fahrenheit)
case 2:
fmt.Print("Enter temperature in Fahrenheit: ")
_, err := fmt.Scan(&temp)
if err != nil {
fmt.Println("Invalid input. Please enter a valid number.")
os.Exit(1)
}
celsius := fahrenheitToCelsius(temp)
fmt.Printf("%.2f Fahrenheit is equal to %.2f Celsius\n", temp, celsius)
default:
fmt.Println("Invalid choice. Please choose either 1 or 2.")
os.Exit(1)
}
}
Program Explanation
The Go program for temperature conversion works as follows:
- Importing Required Packages: The program imports the necessary packages:
fmt
for formatted input/output operations.os
to exit the program if the input is invalid.strconv
is used for string to number conversions (though not used directly in this program, it could be useful for future enhancements).
- Conversion Functions:
Two functions are defined to handle the conversion:celsiusToFahrenheit()
: Converts a temperature in Celsius to Fahrenheit.fahrenheitToCelsius()
: Converts a temperature in Fahrenheit to Celsius.
- User Input:
The user is first asked to choose between converting Celsius to Fahrenheit or Fahrenheit to Celsius. Based on their choice, the program asks them to input the temperature value, and it will output the converted value. - Input Validation:
The program ensures that the input is a valid number and checks for any invalid choices. - Output:
The program outputs the converted temperature with two decimal places for clarity.
How to Run the Program
- Install Go: If you don’t already have Go installed on your computer, download and install it from the official Go website: Go Downloads.
- Write the Code: Copy the Go program code above into a text file and save it with the name
temperature_conversion.go
. - Compile and Run:
- Open a terminal or command prompt.
- Navigate to the directory where the
temperature_conversion.go
file is saved. - Run the following command to compile the program:
go run temperature_conversion.go
- Follow the on-screen prompts to choose the conversion type and input the temperature.
Conclusion
This simple Go program effectively demonstrates how to convert temperatures between Celsius and Fahrenheit. It’s a great starting point for those looking to understand basic Go syntax and how to handle user input and output in Go programs.