app := cli.NewApp() app.Action = func(c *cli.Context) error { fmt.Println("Hello, world!") return nil } err := app.Run(os.Args) if err != nil { log.Fatal(err) }
app := cli.NewApp() app.Flags = []cli.Flag{ cli.StringFlag{ Name: "name", Value: "World", Usage: "A name to say hello to.", }, } app.Action = func(c *cli.Context) error { name := c.String("name") fmt.Printf("Hello, %s! How are you?\n", name) return nil } err := app.Run(os.Args) if err != nil { log.Fatal(err) }This code is similar to the first example, but it adds a command line flag to the app. The flag is a string flag called "name" with a default value of "World" and a usage message. The app's action now uses this flag to say hello to the specified name instead of always saying "Hello, world!" like the first example. In summary, the code examples demonstrate the use of the Context data structure from the github.com.codegangsta/cli package to build command line interfaces in Go.