Simple Calculator Program in Go
This Go program implements a basic calculator with operations for addition, subtraction, multiplication, and division.
Go Code:
/*
A simple calculator program in Go.
*/
package main
import (
"fmt"
)
// Function to add two numbers
func add(num1, num2 float64) float64 {
return num1 + num2
}
// Function to subtract two numbers
func subtract(num1, num2 float64) float64 {
return num1 - num2
}
// Function to multiply two numbers
func multiply(num1, num2 float64) float64 {
return num1 * num2
}
// Function to divide two numbers
func divide(num1, num2 float64) (float64, error) {
if num2 == 0 {
return 0, fmt.Errorf("Cannot divide by zero")
}
return num1 / num2, nil
}
func main() {
// Example usage
num1 := 10.5
num2 := 5.2
// Perform operations
sum := add(num1, num2)
difference := subtract(num1, num2)
product := multiply(num1, num2)
quotient, err := divide(num1, num2)
// Check for divide by zero error
if err != nil {
fmt.Println("Error:", err)
return
}
// Output results
fmt.Println("Sum:", sum)
fmt.Println("Difference:", difference)
fmt.Println("Product:", product)
fmt.Println("Quotient:", quotient)
}
Explanation:
The Go code above defines a simple calculator program with the following components:
- Function Definitions: Functions (
add
,subtract
,multiply
,divide
) are defined to perform basic arithmetic operations. - Function Documentation: Each function includes comments to describe their purpose, parameters, return values, and potential errors.
- Error Handling: The
divide
function returns an error if the divisor is zero, usingfmt.Errorf
to create a descriptive error message. - Example Usage: Variables
num1
andnum2
are initialized with example values, and the functions are called to perform addition, subtraction, multiplication, and division operations. - Output: The results of each operation (
sum
,difference
,product
,quotient
) are printed to the console usingfmt.Println
.
Usage:
To use this calculator program:
- Edit the values of
num1
andnum2
to input different numbers. - Run the Go program to perform addition, subtraction, multiplication, and division operations based on the input values.
Explanation:
- Function Definitions:
- Functions (
add
,subtract
,multiply
,divide
) are explained with their roles in performing arithmetic operations in Go.
- Functions (
- Function Documentation:
- Each function includes comments to describe their purpose, parameters, return values, and potential errors that may occur.
- Error Handling:
- The
divide
function returns an error if the divisor (num2
) is zero, demonstrating error handling in Go usingfmt.Errorf
.
- The
- Example Usage:
- Variables
num1
andnum2
are initialized with example values, and the functions (add
,subtract
,multiply
,divide
) are called to perform operations.
- Variables
- Output:
- The results of each operation (
sum
,difference
,product
,quotient
) are printed to the console usingfmt.Println
.
- The results of each operation (
- Usage Instructions:
- Instructions are provided on how to use the calculator program by editing the input variables, running the Go program, and observing the results of arithmetic calculations.