0
내가 가서 언어의 코드를 작성하고있는 코브라 라이브러리를 사용하여 한 줄로 입력을 허용하는 방법, 현재 입력 임주는이 : 코브라 잘 알고있는 사람이 그에서가 코브라를 사용하여 이동
Calc add
Enter the Number of inputs
2
Enter the Numbers
2
4
Output: Sum is : 6
, Calc는 내 프로젝트이고 add 명령은 Im을 사용합니다. 입력을 Calc add N2 2 4
(한 줄에 입력)하고 출력을 표시해야합니다. 여기서 N은 입력 수를 식별하는 변수이고 2는 4입니다. 추가 할 숫자. 추가 명령
CODE :
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Short: "Addition value of given Numbers",
Run: func(cmd *cobra.Command, args []string) {
length := 0
fmt.Println("Enter the number of inputs")
fmt.Scanln(&length)
fmt.Println("Enter the inputs")
numbers := make([]int, length)
for i := 0; i < length; i++ {
fmt.Scanln(&numbers[i])
}
fmt.Println(numbers)
sum:=0
for _, numbers := range numbers {
sum += numbers
}
fmt.Println("The Sum :",sum)
},
}
func init() {
RootCmd.AddCommand(addCmd)
}
P 이것은 당신의 목적을 성취 할 것이다
알 수없는 플래그를 말하는 오류를 보여줍니다 못할 이유를 이해 @ AerofoilKite – Manu
@Manu을, 오류가 계속됩니다. 당신은 깃발을 통과해야합니다 - 입력 – aerokite
그것을 고쳤습니다. @AerofoilKite – Manu