英文:
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/cobra
。go 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论