Tag Archives: go cobra Error

[Solved] go cobra Error: required flag(s) “pkg-name” not set

Cobra is a Golang package that provides a simple interface to create command line programs. At the same time, Cobra is also an application that is used to generate application frameworks to develop applications based on Cobra.

Use cobra init the command to initialize framework, but found an error:

Error: required flag(s) " pkg-name " not set

After checking the official documentation, I found that after the Cobra version is updated, a mandatory parameter needs to be added --pkg-name, which is the package imported by the main function by default.

Updates to the Cobra generator have now decoupled it from the GOPATH. As such --pkg-nameis required.

So use the following command to initialize:

Create the directory cobra_demo first, enter and then initialize

cobra init --pkg-name cobra_demo

After the initialization is successful, the following prompt message appears:

Your Cobra application is ready at

 

At this time, the project structure should be as follows:

cobra_demo/ 
    cmd / 
    root.go
    main.go
    LICENSE