附加一個Go庫的實現,相較于Python,Go的實現更較為日常,不需要額外增加setup.py類的文件去額外定義,計算和并發的性能更加。
1. 創建 Go 模塊項目結構
首先創建完整的項目結構:
gomathlib/
├── go.mod
├── go.sum
├── core/
│ ├── arithmetic.go
│ └── calculator.go
├── advanced/
│ ├── functions.go
│ └── statistics.go
├── constants/
│ └── constants.go
├── examples/
│ ├── basic_usage.go
│ └── advanced_usage.go
├── tests/
│ ├── core_test.go
│ ├── advanced_test.go
│ └── constants_test.go
└── README.md
2. 初始化 Go 模塊
# 創建項目目錄
mkdir gomathlib
cd gomathlib# 初始化 Go 模塊
go mod init github.com/yourusername/gomathlib# 創建目錄結構
mkdir -p core advanced constants examples tests
3. 核心代碼實現
3.1 go.mod
module github.com/yourusername/gomathlibgo 1.21require (github.com/stretchr/testify v1.8.4
)
3.2 core/arithmetic.go
package coreimport ("errors""math"
)// Arithmetic 提供基礎算術運算
type Arithmetic struct {precision int
}// NewArithmetic 創建新的算術運算實例
func NewArithmetic(precision int) *Arithmetic {return &Arithmetic{precision: precision}
}// Add 加法運算,支持多個參數
func (a *Arithmetic) Add(numbers ...float64) float64 {var sum float64for _, num := range numbers {sum += num}return a.round(sum)
}// Subtract 減法運算
func (a *Arithmetic) Subtract(aVal, bVal float64) float64 {return a.round(aVal - bVal)
}// Multiply 乘法運算,支持多個參數
func (a *Arithmetic) Multiply(numbers ...float64) float64 {product := 1.0for _, num := range numbers {product *= num}return a.round(product)
}// Divide 除法運算
func (a *Arithmetic) Divide(dividend, divisor float64) (float64, error) {if divisor == 0 {return 0, errors.New("division by zero is not allowed")}return a.round(dividend / divisor), nil
}// Power 冪運算
func (a *Arithmetic) Power(base, exponent float64) float64 {return a.round(math.Pow(base, exponent))
}// Sqrt 平方根運算
func (a *Arithmetic) Sqrt(number float64) (float64, error) {if number < 0 {return 0, errors.New("cannot calculate square root of negative number")}return a.round(math.Sqrt(number)), nil
}// round 四舍五入到指定精度
func (a *Arithmetic) round(value float64) float64 {if a.precision < 0 {return value}shift := math.Pow(10, float64(a.precision))return math.Round(value*shift) / shift
}
3.3 core/calculator.go
package coreimport ("fmt""strings"
)// Calculator 提供鏈式計算功能
type Calculator struct {result float64history []stringprecision int
}// NewCalculator 創建新的計算器實例
func NewCalculator(initialValue float64, precision int) *Calculator {return &Calculator{result: initialValue,history: []string{fmt.Sprintf("Initial value: %.2f", initialValue)},precision: precision,}
}// Add 加法操作
func (c *Calculator) Add(value float64) *Calculator {c.result += valuec.history = append(c.history, fmt.Sprintf("+ %.2f", value))return c
}// Subtract 減法操作
func (c *Calculator) Subtract(value float64) *Calculator {c.result -= valuec.history = append(c.history, fmt.Sprintf("- %.2f", value))return c
}// Multiply 乘法操作
func (c *Calculator) Multiply(value float64) *Calculator {c.result *= valuec.history = append(c.history, fmt.Sprintf("* %.2f", value))return c
}// Divide 除法操作
func (c *Calculator) Divide(value float64) (*Calculator, error) {if value == 0 {return nil, fmt.Errorf("division by zero")}c.result /= valuec.history = append(c.history, fmt.Sprintf("/ %.2f", value))return c, nil
}// GetResult 獲取當前結果
func (c *Calculator) GetResult() float64 {// 使用內置的round方法shift := math.Pow(10, float64(c.precision))return math.Round(c.result*shift) / shift
}// Clear 清除計算歷史
func (c *Calculator) Clear() *Calculator {c.result = 0c.history = []string{"Cleared"}return c
}// GetHistory 獲取計算歷史
func (c *Calculator) GetHistory() []string {return c.history
}// String 返回計算歷史的字符串表示
func (c *Calculator) String() string {return strings.Join(c.history, " → ")
}
3.4 advanced/functions.go
package advancedimport ("errors""math"
)// MathFunctions 提供高級數學函數
type MathFunctions struct{}// NewMathFunctions 創建新的數學函數實例
func NewMathFunctions() *MathFunctions {return &MathFunctions{}
}// Factorial 計算階乘
func (mf *MathFunctions) Factorial(n int) (int, error) {if n < 0 {return 0, errors.New("factorial is not defined for negative numbers")}if n == 0 {return 1, nil}result := 1for i := 1; i <= n; i++ {result *= i}return result, nil
}// IsPrime 判斷是否為質數
func (mf *MathFunctions) IsPrime(n int) bool {if n <= 1 {return false}if n <= 3 {return true}if n%2 == 0 || n%3 == 0 {return false}for i := 5; i*i <= n; i += 6 {if n%i == 0 || n%(i+2) == 0 {return false}}return true
}// Fibonacci 生成斐波那契數列
func (mf *MathFunctions) Fibonacci(n int) ([]int, error) {if n < 0 {return nil, errors.New("n must be non-negative")}if n == 0 {return []int{}, nil}if n == 1 {return []int{0}, nil}if n == 2 {return []int{0, 1}, nil}fib := make([]int, n)fib[0] = 0fib[1] = 1for i := 2; i < n; i++ {fib[i] = fib[i-1] + fib[i-2]}return fib, nil
}// Log 計算對數
func (mf *MathFunctions) Log(number, base float64) (float64, error) {if number <= 0 || base <= 0 || base == 1 {return 0, errors.New("invalid arguments for logarithm")}return math.Log(number) / math.Log(base), nil
}// Sin 計算正弦值(弧度)
func (mf *MathFunctions) Sin(radians float64) float64 {return math.Sin(radians)
}// Cos 計算余弦值(弧度)
func (mf *MathFunctions) Cos(radians float64) float64 {return math.Cos(radians)
}
3.5 advanced/statistics.go
package advancedimport ("errors""math""sort"
)// Statistics 提供統計計算功能
type Statistics struct{}// NewStatistics 創建新的統計實例
func NewStatistics() *Statistics {return &Statistics{}
}// Mean 計算平均值
func (s *Statistics) Mean(numbers []float64) (float64, error) {if len(numbers) == 0 {return 0, errors.New("empty slice provided")}sum := 0.0for _, num := range numbers {sum += num}return sum / float64(len(numbers)), nil
}// Median 計算中位數
func (s *Statistics) Median(numbers []float64) (float64, error) {if len(numbers) == 0 {return 0, errors.New("empty slice provided")}sorted := make([]float64, len(numbers))copy(sorted, numbers)sort.Float64s(sorted)n := len(sorted)if n%2 == 0 {return (sorted[n/2-1] + sorted[n/2]) / 2, nil}return sorted[n/2], nil
}// Mode 計算眾數
func (s *Statistics) Mode(numbers []float64) ([]float64, error) {if len(numbers) == 0 {return nil, errors.New("empty slice provided")}frequency := make(map[float64]int)for _, num := range numbers {frequency[num]++}maxFreq := 0for _, freq := range frequency {if freq > maxFreq {maxFreq = freq}}var modes []float64for num, freq := range frequency {if freq == maxFreq {modes = append(modes, num)}}return modes, nil
}// StandardDeviation 計算標準差
func (s *Statistics) StandardDeviation(numbers []float64) (float64, error) {if len(numbers) < 2 {return 0, errors.New("at least two numbers required for standard deviation")}mean, err := s.Mean(numbers)if err != nil {return 0, err}sumSq := 0.0for _, num := range numbers {diff := num - meansumSq += diff * diff}variance := sumSq / float64(len(numbers)-1)return math.Sqrt(variance), nil
}// Variance 計算方差
func (s *Statistics) Variance(numbers []float64) (float64, error) {if len(numbers) < 2 {return 0, errors.New("at least two numbers required for variance")}mean, err := s.Mean(numbers)if err != nil {return 0, err}sumSq := 0.0for _, num := range numbers {diff := num - meansumSq += diff * diff}return sumSq / float64(len(numbers)-1), nil
}
3.6 constants/constants.go
package constants// MathConstants 包含常用數學常量
type MathConstants struct {PI float64E float64GoldenRatio float64EulerMascheroni float64LightSpeed float64
}// NewMathConstants 創建數學常量實例
func NewMathConstants() *MathConstants {return &MathConstants{PI: 3.14159265358979323846,E: 2.71828182845904523536,GoldenRatio: 1.61803398874989484820,EulerMascheroni: 0.57721566490153286060,LightSpeed: 299792458, // m/s}
}// 包級常量
var (PI = 3.14159265358979323846E = 2.71828182845904523536GoldenRatio = 1.61803398874989484820
)
4. 使用教程
4.1 安裝依賴
# 下載依賴
go mod tidy# 或者手動安裝測試框架
go get github.com/stretchr/testify
4.2 基本使用示例 examples/basic_usage.go
package mainimport ("fmt""log""github.com/yourusername/gomathlib/core""github.com/yourusername/gomathlib/advanced""github.com/yourusername/gomathlib/constants"
)func main() {fmt.Println("=== GoMathLib 基本使用示例 ===")// 基礎算術運算arithmetic := core.NewArithmetic(4)fmt.Println("\n1. 基礎運算:")fmt.Printf("加法: %.4f\n", arithmetic.Add(1.2345, 2.3456))fmt.Printf("乘法: %.4f\n", arithmetic.Multiply(3, 4, 5))result, err := arithmetic.Divide(22, 7)if err != nil {log.Fatal(err)}fmt.Printf("除法: %.4f\n", result)// 鏈式計算器fmt.Println("\n2. 鏈式計算器:")calc := core.NewCalculator(10, 2)calcResult, err := calc.Add(5).Multiply(3).Subtract(2).Divide(4)if err != nil {log.Fatal(err)}fmt.Printf("計算結果: %.2f\n", calcResult.GetResult())fmt.Printf("計算歷史: %s\n", calcResult.String())// 高級函數fmt.Println("\n3. 高級函數:")mathFuncs := advanced.NewMathFunctions()factorial, err := mathFuncs.Factorial(5)if err != nil {log.Fatal(err)}fmt.Printf("5的階乘: %d\n", factorial)fmt.Printf("17是質數: %t\n", mathFuncs.IsPrime(17))fib, err := mathFuncs.Fibonacci(10)if err != nil {log.Fatal(err)}fmt.Printf("斐波那契數列: %v\n", fib)// 數學常量fmt.Println("\n4. 數學常量:")mathConsts := constants.NewMathConstants()fmt.Printf("圓周率: %.10f\n", mathConsts.PI)fmt.Printf("自然常數e: %.10f\n", mathConsts.E)fmt.Printf("黃金比例: %.10f\n", mathConsts.GoldenRatio)fmt.Printf("光速: %.0f m/s\n", mathConsts.LightSpeed)
}
5. 測試用例
5.1 tests/core_test.go
package testsimport ("testing""github.com/yourusername/gomathlib/core""github.com/stretchr/testify/assert"
)func TestArithmetic(t *testing.T) {arithmetic := core.NewArithmetic(4)t.Run("Test Addition", func(t *testing.T) {result := arithmetic.Add(1, 2, 3, 4)assert.Equal(t, 10.0, result)})t.Run("Test Multiplication", func(t *testing.T) {result := arithmetic.Multiply(2, 3, 4)assert.Equal(t, 24.0, result)})t.Run("Test Division", func(t *testing.T) {result, err := arithmetic.Divide(10, 2)assert.NoError(t, err)assert.Equal(t, 5.0, result)})t.Run("Test Division By Zero", func(t *testing.T) {_, err := arithmetic.Divide(10, 0)assert.Error(t, err)})t.Run("Test Precision", func(t *testing.T) {result := arithmetic.Add(1.23456, 2.34567)assert.Equal(t, 3.5802, result) // Rounded to 4 decimal places})
}func TestCalculator(t *testing.T) {t.Run("Test Calculator Chain", func(t *testing.T) {calc := core.NewCalculator(10, 2)result, err := calc.Add(5).Multiply(3).Subtract(2).Divide(4)assert.NoError(t, err)assert.Equal(t, 9.75, result.GetResult())})t.Run("Test Calculator Division By Zero", func(t *testing.T) {calc := core.NewCalculator(10, 2)_, err := calc.Divide(0)assert.Error(t, err)})t.Run("Test Calculator History", func(t *testing.T) {calc := core.NewCalculator(0, 2)calc.Add(5).Multiply(2)history := calc.GetHistory()assert.Len(t, history, 3) // Initial + Add + Multiply})
}
5.2 tests/advanced_test.go
package testsimport ("testing""github.com/yourusername/gomathlib/advanced""github.com/stretchr/testify/assert"
)func TestMathFunctions(t *testing.T) {mathFuncs := advanced.NewMathFunctions()t.Run("Test Factorial", func(t *testing.T) {result, err := mathFuncs.Factorial(5)assert.NoError(t, err)assert.Equal(t, 120, result)})t.Run("Test Factorial Negative", func(t *testing.T) {_, err := mathFuncs.Factorial(-1)assert.Error(t, err)})t.Run("Test IsPrime", func(t *testing.T) {assert.True(t, mathFuncs.IsPrime(2))assert.True(t, mathFuncs.IsPrime(17))assert.False(t, mathFuncs.IsPrime(1))assert.False(t, mathFuncs.IsPrime(15))})t.Run("Test Fibonacci", func(t *testing.T) {result, err := mathFuncs.Fibonacci(10)assert.NoError(t, err)expected := []int{0, 1, 1, 2, 3, 5, 8, 13, 21, 34}assert.Equal(t, expected, result)})
}func TestStatistics(t *testing.T) {stats := advanced.NewStatistics()numbers := []float64{1, 2, 3, 4, 5, 5, 6}t.Run("Test Mean", func(t *testing.T) {result, err := stats.Mean(numbers)assert.NoError(t, err)assert.Equal(t, 3.7142857142857144, result)})t.Run("Test Median", func(t *testing.T) {result, err := stats.Median(numbers)assert.NoError(t, err)assert.Equal(t, 4.0, result)})t.Run("Test Mode", func(t *testing.T) {result, err := stats.Mode(numbers)assert.NoError(t, err)assert.Equal(t, []float64{5}, result)})t.Run("Test Empty Slice", func(t *testing.T) {_, err := stats.Mean([]float64{})assert.Error(t, err)})
}
5.3 tests/constants_test.go
package testsimport ("testing""github.com/yourusername/gomathlib/constants""github.com/stretchr/testify/assert"
)func TestConstants(t *testing.T) {mathConsts := constants.NewMathConstants()t.Run("Test PI", func(t *testing.T) {assert.Equal(t, 3.14159265358979323846, mathConsts.PI)assert.Equal(t, mathConsts.PI, constants.PI)})t.Run("Test E", func(t *testing.T) {assert.Equal(t, 2.71828182845904523536, mathConsts.E)assert.Equal(t, mathConsts.E, constants.E)})t.Run("Test GoldenRatio", func(t *testing.T) {assert.Equal(t, 1.61803398874989484820, mathConsts.GoldenRatio)assert.Equal(t, mathConsts.GoldenRatio, constants.GoldenRatio)})
}
6. 運行測試和示例
6.1 運行測試
# 運行所有測試
go test ./tests/ -v# 運行特定測試
go test ./tests/ -run TestArithmetic -v# 運行帶覆蓋率的測試
go test ./tests/ -cover -v# 生成HTML覆蓋率報告
go test ./tests/ -coverprofile=coverage.out
go tool cover -html=coverage.out
6.2 運行示例
# 運行基本使用示例
go run examples/basic_usage.go# 構建示例
go build -o examples/basic_usage examples/basic_usage.go
./examples/basic_usage
7. 發布和使用
7.1 在其他項目中使用
package mainimport ("fmt""github.com/yourusername/gomathlib/core""github.com/yourusername/gomathlib/advanced"
)func main() {// 使用算術運算math := core.NewArithmetic(2)fmt.Printf("Result: %.2f\n", math.Add(1.23, 4.56))// 使用高級函數adv := advanced.NewMathFunctions()isPrime := adv.IsPrime(29)fmt.Printf("Is 29 prime? %t\n", isPrime)
}
7.2 在其他項目中引用
# 在其他Go項目中引用
go mod init myapp
go mod edit -require github.com/yourusername/gomathlib@v0.1.0
go mod edit -replace github.com/yourusername/gomathlib=../gomathlib
go mod tidy
8. 創建文檔
8.1 生成Godoc
# 啟動本地Godoc服務器
godoc -http=:6060# 然后在瀏覽器訪問: http://localhost:6060/pkg/github.com/yourusername/gomathlib/
8.2 README.md
# GoMathLib一個功能強大的Go數學運算庫,提供基礎運算、高級函數和統計計算。## 安裝```bash
go get github.com/yourusername/gomathlib
快速開始
package mainimport ("fmt""github.com/yourusername/gomathlib/core"
)func main() {math := core.NewArithmetic(2)result := math.Add(1.23, 4.56)fmt.Printf("Result: %.2f\n", result)
}
功能特性
- 基礎四則運算
- 鏈式計算器
- 高級數學函數(階乘、質數判斷、斐波那契數列)
- 統計計算(平均值、中位數、眾數、標準差)
- 數學常量