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

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

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。

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. )
  6. func add(m ...int) int {
  7. sum := 0
  8. for _, a := range m {
  9. sum += a
  10. }
  11. return sum
  12. }
  13. func sub(m ...int) int {
  14. sub := m[0]
  15. for _, a := range m[1:] {
  16. sub -= a
  17. }
  18. return sub
  19. }
  20. func mul(m ...float32) float32 {
  21. pro := float32(1)
  22. for _, a := range m {
  23. pro *= a
  24. }
  25. return pro
  26. }
  27. func div(m ...float32) float32 {
  28. quo := m[0]
  29. for _, a := range m[1:] {
  30. quo /= a
  31. }
  32. return quo
  33. }
  34. var i int
  35. func display() {
  36. fmt.Println("选择操作:1:加法 2:减法 3:乘法 4:除法")
  37. fmt.Scanln(&i)
  38. }
  39. func main() {
  40. display()
  41. var v1,v2 int
  42. fmt.Println("输入两个数字并按回车")
  43. fmt.Scanln(&v1)
  44. fmt.Scanln(&v2)
  45. switch i {
  46. case 1:
  47. fmt.Println(add(v1,v2))
  48. case 2:
  49. fmt.Println(sub(v1,v2))
  50. case 3:
  51. fmt.Println(mul(float32(v1),float32(v2)))
  52. case 4:
  53. fmt.Println(div(float32(v1),float32(v2)))
  54. }
  55. }
英文:

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.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. )
  6. func add(m ...int) int {
  7. sum := 0
  8. for _, a := range m {
  9. sum += a
  10. }
  11. return sum
  12. }
  13. func sub(m ...int) int {
  14. sub := m[0]
  15. for _, a := range m[1:] {
  16. sub -= a
  17. }
  18. return sub
  19. }
  20. func mul(m ...float32) float32 {
  21. pro := float32(1)
  22. for _, a := range m {
  23. pro *= a
  24. }
  25. return pro
  26. }
  27. func div(m ...float32) float32 {
  28. quo := m[0]
  29. for _, a := range m[1:] {
  30. quo /= a
  31. }
  32. return quo
  33. }
  34. var i int
  35. func display() {
  36. fmt.Println("Choose the operation : 1:Addition 2:Subtration 3:Multiplication 4:Division ")
  37. fmt.Scanln(&i)
  38. }
  39. func main() {
  40. display()
  41. var v1,v2 int
  42. fmt.Println("Enter 2 numbers with enter")
  43. fmt.Scanln(&v1)
  44. fmt.Scanln(&v2)
  45. switch i {
  46. case 1:
  47. fmt.Println(add(v1,v2))
  48. case 2:
  49. fmt.Println(sub(v1,v2))
  50. case 3:
  51. fmt.Println(mul(float32(v1),float32(v2)))
  52. case 4:
  53. fmt.Println(div(float32(v1),float32(v2)))
  54. }
  55. }

答案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:

确定