I have written a program to calculate basic arithmetic functions using interfaces in Go language.
I want to do the same using COBRA commands. Here is some Go code using interfaces.
I have also implemented the arithmetic functions using cobra
commands
without the interfaces.
this is addition using cobra commands without using the concept of interfaces, I want to implement the same using cobra commands.
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"os"
"strconv"
)
func addCmd() *cobra.Command {
var input int
c := &cobra.Command{
Use: "add",
Short: "Addition value of given Numbers",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != input {
fmt.Println(fmt.Sprintf("You need to provide %v number to sum up", input))
os.Exit(1)
}
numbers := make([]int, input)
for i := 0; i < input; i++ {
num, _ := strconv.Atoi(args[i])
numbers[i] = num
}
sum := 0
for _, numbers := range numbers {
sum += numbers
}
fmt.Println("The Sum :", sum)
},
}
c.Flags().IntVar(&input, "input", 0, "Number of input")
return c
}
func init() {
cmd := addCmd()
RootCmd.AddCommand(cmd)
}
Can someone help me out with the interfaces and the cobra commands.