codinggirl
codinggirl

Reputation: 159

I'm trying to integrate cobra in my program

I was referring to spf13/cobra.

I downloaded the cobra package using go get github.com/spf13/cobra/cobra and imported "github.com/spf13/cobra" in my program and then installed it using go install github.com/spf13/cobra/cobra.

This is my program - It is a calculator which can be implemented of number of inputs , but for now only 2 are taken from the user. I wanted to use cobra in this program.

    package main

    import (
                "fmt"
                "github.com/spf13/cobra"
            )


    func add(m ...int) int {
        sum := 0
        for _, a := range m {
            sum += a
        }
        return sum
    }
    func sub(m ...int) int {
        sub := m[0]
        for _, a := range m[1:] {
            sub -= a
        }
        return sub
    }
    func mul(m ...float32) float32 {
        pro := float32(1)
        for _, a := range m {
            pro *= a
        }
        return pro
    }
    func div(m ...float32) float32 {
        quo := m[0]
        for _, a := range m[1:] {
            quo /= a
        }
        return quo
    }

    var i int

    func display() {

        fmt.Println("Choose the operation : 1:Addition 2:Subtration 3:Multiplication 4:Division ")
        fmt.Scanln(&i)
    }

    func main() {

        display()

        var v1,v2 int
        fmt.Println("Enter 2 numbers with enter")
        fmt.Scanln(&v1)
        fmt.Scanln(&v2)

        switch i {
        case 1:
            fmt.Println(add(v1,v2))
        case 2:
            fmt.Println(sub(v1,v2))
        case 3:
            fmt.Println(mul(float32(v1),float32(v2)))
        case 4:
      fmt.Println(div(float32(v1),float32(v2))) 
         }
    }  

Upvotes: 0

Views: 240

Answers (1)

hiy
hiy

Reputation: 459

You need to run go get github.com/spf13/cobra/cobra first. go install can only install packages you've already downloaded, go get downloads and installs a package.

Upvotes: 1

Related Questions