Golang
Golang

 

 

Introduction

Unit conversion is a common task in various applications, such as engineering, science, and everyday calculations. This program demonstrates how to convert between different units of measurement, including length (meters, kilometers, miles), weight (kilograms, pounds), and volume (liters, gallons). The implementation uses the Go programming language, providing a simple and interactive way to perform these conversions.

Objective

The goal of this program is to create a command-line utility in Go that allows users to convert between different units of length, weight, and volume. The program will accept user input for the source unit, target unit, and value, then compute and display the result.

Code

        package main

        import (
            "fmt"
            "os"
        )

        func main() {
            fmt.Println("Unit Converter: Length, Weight, Volume")
            fmt.Println("Select a category:")
            fmt.Println("1. Length (meters to kilometers, miles)")
            fmt.Println("2. Weight (kilograms to pounds)")
            fmt.Println("3. Volume (liters to gallons)")

            var choice int
            fmt.Print("Enter your choice (1-3): ")
            fmt.Scanln(&choice)

            switch choice {
            case 1:
                convertLength()
            case 2:
                convertWeight()
            case 3:
                convertVolume()
            default:
                fmt.Println("Invalid choice. Exiting.")
                os.Exit(1)
            }
        }

        func convertLength() {
            fmt.Println("Length Conversion: meters to kilometers, miles")
            fmt.Print("Enter value in meters: ")
            var meters float64
            fmt.Scanln(&meters)

            kilometers := meters / 1000
            miles := meters / 1609.34

            fmt.Printf("%.2f meters is %.2f kilometers or %.2f miles.\n", meters, kilometers, miles)
        }

        func convertWeight() {
            fmt.Println("Weight Conversion: kilograms to pounds")
            fmt.Print("Enter value in kilograms: ")
            var kilograms float64
            fmt.Scanln(&kilograms)

            pounds := kilograms * 2.20462
            fmt.Printf("%.2f kilograms is %.2f pounds.\n", kilograms, pounds)
        }

        func convertVolume() {
            fmt.Println("Volume Conversion: liters to gallons")
            fmt.Print("Enter value in liters: ")
            var liters float64
            fmt.Scanln(&liters)

            gallons := liters * 0.264172
            fmt.Printf("%.2f liters is %.2f gallons.\n", liters, gallons)
        }

Explanation

The program is structured as follows:

  1. Category Selection: The user is prompted to choose a category: Length, Weight, or Volume.
  2. Conversion Functions: Each category has a dedicated function (convertLength, convertWeight, convertVolume) to handle specific unit conversions.
  3. Interactive Input: The user provides input for the value to be converted, and the program calculates the result using predefined formulas.
  4. Error Handling: Invalid category choices are handled gracefully with an error message and program termination.

How to Run

  1. Install Go from the official Go website.
  2. Copy the code into a file named unit_converter.go.
  3. Run the program using the command:
                    go run unit_converter.go
    
  4. Follow the on-screen instructions to perform unit conversions.
© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)