package main import ( "github.com/mitchellh/cli" "github.com/mitchellh/cli/ui" ) func main() { // Create a new Ui object to output to stdout u := &cli.BasicUi{ Writer: ui.BasicColorizationWriter{}, ErrorWriter: ui.BasicColorizationWriter{}, } // Output a message to the user u.Info(ui.Colorize("Hello, world!", ui.GREEN)) }
package main import ( "github.com/mitchellh/cli" "github.com/mitchellh/cli/ui" "os" ) func main() { // Open a file to output to f, err := os.Create("output.txt") if err != nil { panic(err) } defer f.Close() // Create a new Ui object to output to the file u := &cli.BasicUi{ Writer: ui.BasicColorizationWriter{Writer: f}, ErrorWriter: ui.BasicColorizationWriter{Writer: f}, } // Output a message to the file u.Info(ui.Colorize("Hello, world!", ui.GREEN)) }In this example, we import the cli, ui, and os packages. We then open a file to output to using os.Create, and close it using defer. We create a new cli.BasicUi object that uses ui.BasicColorizationWriter to output colored text to the file, and pass the file object as the Writer for both the Writer and ErrorWriter fields. Finally, we call u.Info to output a message to the file. In summary, the go github.com/mitchellh/cli package provides a library to build CLIs in Go, and the ui sub-package provides a simple way to output information to the user. The examples above demonstrate how to output text to stdout and to a file, respectively, using the ui sub-package.