72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
// Package utils provides utility functions for common operations.
|
|
//
|
|
// This package includes functions for string manipulation,
|
|
// numerical calculations, and file operations.
|
|
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
// Trim removes whitespace from both ends of a string.
|
|
//
|
|
// s: The string to trim
|
|
//
|
|
// Returns: The trimmed string
|
|
func Trim(s string) string {
|
|
return strings.TrimSpace(s)
|
|
}
|
|
|
|
// Add adds two integers and returns their sum.
|
|
//
|
|
// a: First integer
|
|
// b: Second integer
|
|
//
|
|
// Returns: The sum of a and b
|
|
func Add(a, b int) int {
|
|
return a + b
|
|
}
|
|
|
|
// Divide performs integer division.
|
|
//
|
|
// numerator: The number to divide
|
|
// denominator: The divisor
|
|
//
|
|
// Returns: The quotient, or an error if denominator is zero
|
|
//
|
|
// Raises: error if denominator is zero
|
|
func Divide(numerator, denominator int) (int, error) {
|
|
if denominator == 0 {
|
|
return 0, errors.New("division by zero")
|
|
}
|
|
return numerator / denominator, nil
|
|
}
|
|
|
|
// Greet returns a personalized greeting message.
|
|
//
|
|
// name: The name to include in the greeting
|
|
// formal: Whether to use formal greeting
|
|
//
|
|
// Returns: A greeting string
|
|
func Greet(name string, formal bool) string {
|
|
if formal {
|
|
return "Good day, " + name + "."
|
|
}
|
|
return "Hi, " + name + "!"
|
|
}
|
|
|
|
// ProcessList processes a list of strings with the given function.
|
|
//
|
|
// items: The list of strings to process
|
|
// fn: The function to apply to each item
|
|
//
|
|
// Returns: A new list with processed items
|
|
func ProcessList(items []string, fn func(string) string) []string {
|
|
result := make([]string, len(items))
|
|
for i, item := range items {
|
|
result[i] = fn(item)
|
|
}
|
|
return result
|
|
}
|