Creating a new project

Let's create a new project. These instructions make no assumptions about where you like to write your Go code, so it's a bit vague as to where you're making these directories by design.

Create a new golang project where you like to store your projects (if you're using gomodules), or within your $GOPATH (somewhere like $GOPATH/src/github.com/<yourusername>/).

mkdir math

Initialize gomodules in this path by calling either go mod init (if you're within your $GOPATH, or go mod init <module> (if you're working in module mode somewhere else on your filesystem).

Once you're in the math directory, run the new cobra-cli command with a few flags.

cobra-cli init --author "<your name here>"

Example:

cobra-cli init --author "<your name here>"

Response

Your Cobra application is ready at
/Users/me/.go/src/github.com/opdev/cobra-primer/math

Take a look at your folder; you should see several new files that have been scaffolded for you.

.
├── LICENSE
├── cmd
│   └── root.go
├── go.mod
├── go.sum
└── main.go 

Take a look at main.go.

package main

import "github.com/opdev/cobra-primer/math/cmd"

func main() {
	cmd.Execute()
}

Aside from some comments that I've truncated from this file for this book, the overall content is incredibly short.

All that's happening here is that we're importing a local package cmd and running some function defined there called Execute().

At this point, your project should build and run without issue. Try a go run .

$ go run .
A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.

A small blurb of text generated by cobra-cli is returned while executing the so called, Root Command.