/* * init function to check if p, r and t are greater than zero */ funcinit() { println("Main package initialized") if p < 0 { log.Fatal("Principal is less than zero") } if r < 0 { log.Fatal("Rate of interest is less than zero") } if t < 0 { log.Fatal("Duration is less than zero") } }
//Calculate calculates and returns the simple interest for principal p, rate of interest r for time duration t years funcCalculate(p float64, r float64, t float64)float64 { interest := p * (r / 100) * t return interest }
add/add.go
1 2 3 4 5
package add
funcAdd(a int, b int) (num int) { return a + b }
3 执行并调试
输出如下所示
1 2 3 4
Main packageinitialized Simple interest packageinitialized 3 Simple interest is500