Building the Application on Top of the Skeleton
Explore building a command-line calculator in Go by integrating arithmetic functions into a Cobra-based skeleton. Learn to handle command arguments and implement overflow checks for reliable addition and subtraction commands.
We'll cover the following...
We'll cover the following...
Building your application on top of the skeleton
We will use the same code from the previous lesson and just bolt it on the new skeleton. If you want to follow along, you can use the terminal at the bottom of the lesson. First, let’s add the calc package to pkg/calc/calc.go:
package calc
const (
maxUint=^uint(0)
maxInt = int(maxUint >> 1)
minInt = -maxInt - 1
)
func checkAdd(a, b int) {
if a > 0 {
if b > maxInt - a {
panic("overflow!")
}
} else {
if b < minInt - a {
panic("underflow!")
}
}
}
func checkSub(a, b int) {
if a > 0 {
if b < minInt + a {
panic("overflow!")
}
} else {
if b < a - maxInt {
panic("underflow!")
}
}
}
func Add(a, b int, check bool) int {
if ...