If you run my Go program

With “go build main.go”, do you get the same errors I do ?

The error message tells you: You cannot use "0" as an int line 34 of letters.go:

lettersCmd.Flags().IntVarP(
		&Count, "count", "c", "0",
		"A count of random letters",
	)

If you check the documentation of IntVarP, it says the 4th parameter has to be an int. Instead of using "0", use just 0.

Yeah, I was able to figure that one out and it’s been fixed.

I can’t figure out why the other variables are not defined ?

Where are they defined? I see &Count which takes the address of the Count variable, but I don’t actually see the Count variable defined anywhere.

Yup. I was trying to reproduce this tutorial.
Did I miss something ?

https://levelup.gitconnected.com/exploring-go-packages-cobra-fce6c4e331d6

I think the tutorial has typos in it. Where it talks about the verbose flag, it essentially says:

var (
    verbose bool
)

// ...

func init() {
    rootCmd.PersistentFlags().BoolVarP(
        &verbose, "verbose", "v", false,
        "Verbose output",
    )
}

And this part makes sense to me: You have a verbose variable and BoolVarP references that variable.

The letters, numbers and count flags don’t match up, though:

var (
    countFlagLetters int
    langFlagLetters  string
)

// ...

func init() {
    rootCmd.AddCommand(lettersCmd)
    lettersCmd.Flags().IntVarP(
        &Count, "count", "c", 0,
        "A count of random letters",
    )
    lettersCmd.MarkFlagRequired("count")
    
    lettersCmd.Flags().StringVarP(
        &Lang, "lang", "l", "en", 
        "A language. Optional",
     )
}

The actual variable names are countFlagLetters and langFlagLetters, but the code in init is looking for Count and Lang.

Yup. I see it now. that should be the fix.

Thanks.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.