Getting Started#
Let’s build a simple CLI application named greet.
1. Create a Go Module#
mkdir greet
cd greet
go mod init example.com/greet2. Define a Command#
Create main.go and define your command function.
package main
import "fmt"
// Hello is a subcommand `greet hello`
// Prints a greeting.
func Hello(name string) {
fmt.Printf("Hello, %s!\n", name)
}
func main() {
// This will be replaced by the generated code usage,
// but for now you can leave it empty or use it to run the generated cmd.
}3. Configure Generation#
Create a generate.go file to instruct go generate.
package main
//go:generate gosubc generate4. Generate Code#
Run the generator:
go generateThis will create a cmd/greet directory with the generated code.
5. Run Your CLI#
You can now run your CLI using go run:
go run ./cmd/greet hello --name WorldOutput:
Hello, World!