Hone logo
Hone
Problems

Mastering Go Packages: Building a Reusable Calculator Library

This challenge will guide you through the process of creating your own reusable package in Go. You'll build a simple calculator library that can perform basic arithmetic operations. Understanding packages is fundamental to organizing larger Go projects and sharing functionality efficiently.

Problem Description

Your task is to create a Go package named calculator that exposes functions for performing addition, subtraction, multiplication, and division. This package should be importable by other Go programs, allowing them to leverage your calculator functionality without needing to reimplement it.

Key Requirements:

  • Create a new Go module.
  • Define a package named calculator.
  • Implement four exported functions within the calculator package:
    • Add(a, b int) int: Returns the sum of a and b.
    • Subtract(a, b int) int: Returns the difference of a and b.
    • Multiply(a, b int) int: Returns the product of a and b.
    • Divide(a, b int) (int, error): Returns the quotient of a and b. This function should return an error if division by zero is attempted.
  • Ensure all functions intended for external use are exported (start with an uppercase letter).
  • Write a separate main package that imports and uses your calculator package to demonstrate its functionality.

Expected Behavior:

The calculator package should correctly perform arithmetic operations. The Divide function must handle division by zero gracefully by returning a descriptive error. The main package should successfully import and call the functions from your calculator package.

Edge Cases to Consider:

  • Division by zero.
  • Large integer values (within Go's int limits).

Examples

Example 1:

Input (to calculator package functions):

// In main.go
package main

import (
	"fmt"
	"your_module_path/calculator" // Replace with your actual module path
)

func main() {
	sum := calculator.Add(10, 5)
	difference := calculator.Subtract(10, 5)
	product := calculator.Multiply(10, 5)
	quotient, err := calculator.Divide(10, 5)

	fmt.Printf("Sum: %d\n", sum)
	fmt.Printf("Difference: %d\n", difference)
	fmt.Printf("Product: %d\n", product)
	if err == nil {
		fmt.Printf("Quotient: %d\n", quotient)
	}
}

Output:

Sum: 15
Difference: 5
Product: 50
Quotient: 2

Explanation:

The main function calls each of the exported calculator functions with sample inputs and prints their results. The division operation is successful as the divisor is not zero.

Example 2:

Input (to calculator package functions):

// In main.go
package main

import (
	"fmt"
	"your_module_path/calculator" // Replace with your actual module path
)

func main() {
	_, err := calculator.Divide(10, 0) // Attempting division by zero

	if err != nil {
		fmt.Printf("Error: %s\n", err)
	}
}

Output:

Error: division by zero

Explanation:

The Divide function is called with a divisor of zero. It detects this invalid operation and returns a non-nil error object, which the main function then prints.

Constraints

  • The calculator package must be a distinct directory within your Go module.
  • All functions in the calculator package that are intended to be used externally must be exported (start with an uppercase letter).
  • The Divide function must return an error of type error when division by zero occurs.
  • Input integers for Add, Subtract, and Multiply will be within the standard Go int range.
  • The division operation should result in integer division (truncating any remainder).

Notes

  • To initialize a Go module, navigate to your project directory in the terminal and run go mod init your_module_path (replace your_module_path with a unique identifier for your module, e.g., github.com/yourusername/calculator).
  • The calculator package should reside in a subdirectory named calculator within your module.
  • You will need to create a main package in a separate directory (e.g., cmd/main or main) to test your calculator package.
  • Remember to use the error interface for error handling in Go. The errors package can be helpful for creating simple error messages.
  • Think about how you will structure your project directory. A common approach is to have a calculator directory for the package and a separate directory for the main application that uses it.
Loading editor...
go