我正在尝试在我的程序中集成 Cobra。

huangapple go评论183阅读模式
英文:

I'm trying to integrate cobra in my program

问题

我正在参考spf13/cobra

我使用go get github.com/spf13/cobra/cobra下载了cobra包,并在我的程序中导入了"github.com/spf13/cobra",然后使用go install github.com/spf13/cobra/cobra进行安装。

这是我的程序 - 它是一个计算器,可以实现任意数量的输入,但目前只从用户那里获取两个输入。我想在这个程序中使用cobra。

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("选择操作:1:加法 2:减法 3:乘法 4:除法")
	fmt.Scanln(&i)
}

func main() {

	display()
	
	var v1,v2 int
	fmt.Println("输入两个数字并按回车")
	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)))	
	}
}
英文:

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)))	
         }
    }  

答案1

得分: 1

你需要先运行go get github.com/spf13/cobra/cobrago install只能安装你已经下载的包,而go get会下载并安装一个包。

英文:

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.

huangapple
  • 本文由 发表于 2017年1月6日 17:15:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/41502403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定