0Pricing
Go Academy · Lesson

Flags and Persistent Flags

String, int, bool flags and Viper integration

Flags and Persistent Flags is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Local flags

Local flags apply only to the command on which they are defined. Use cmd.Flags():

var port int
serveCmd.Flags().IntVar(&port, "port", 8080, "HTTP server port")

Persistent flags

Persistent flags are inherited by the command and all its subcommands. Use cmd.PersistentFlags():

var verbose bool
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose output")

Short flags

Use the *VarP variants to add a single-letter shorthand:

cmd.Flags().StringVarP(&output, "output", "o", "stdout", "Output destination")

Flag types

Cobra (via pflag) supports: String, Bool, Int, Int64, Float64, StringSlice, Duration, Count, and more. Each has a Var variant (takes a pointer) and a convenience variant (returns pointer).

Required flags

Mark a flag required — Cobra will error if it is not provided:

cmd.MarkFlagRequired("config")

Flag groups

Mark flags as mutually exclusive or required together with MarkFlagsRequiredTogether and MarkFlagsMutuallyExclusive:

cmd.MarkFlagsRequiredTogether("user", "password")
cmd.MarkFlagsMutuallyExclusive("json", "yaml")

StringSlice flag

Accept multiple values as a comma-separated list or repeated flags:

var tags []string
cmd.Flags().StringSliceVar(&tags, "tags", nil, "Tags (comma-separated)")
// Usage: --tags a,b,c or --tags a --tags b

Binding flags to Viper

Bind a cobra flag to a Viper key so the value can also come from env var or config file:

viper.BindPFlag("port", serveCmd.Flags().Lookup("port"))
viper.BindEnv("port", "APP_PORT")

Default value display

Cobra shows the default value in the help text automatically. Provide a clear default that works out of the box for the common case.

Accessing flag values

Access flag values via the pointer you bound, or look them up by name from the command's flag set if the pointer is not accessible:

port, _ := cmd.Flags().GetInt("port")

Changed method

cmd.Flags().Changed("flag-name") returns true only if the user explicitly set the flag. Use it to distinguish "not provided" from "provided with default value".

Quick Check

What is the difference between a local flag and a persistent flag in Cobra?

Recap: Flags and Persistent Flags

Key points:

  • cmd.Flags(): local; cmd.PersistentFlags(): inherited by subcommands
  • *VarP for shorthand; MarkFlagRequired for mandatory flags
  • MarkFlagsRequiredTogether / MarkFlagsMutuallyExclusive for relationships
  • Changed() to detect whether user explicitly set a flag

Frequently asked questions

Is the “Flags and Persistent Flags” lesson free?

Yes — the full text of “Flags and Persistent Flags” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Flags and Persistent Flags”?

String, int, bool flags and Viper integration You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Flags and Persistent Flags” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Cobra Fundamentals
  2. Flags and Persistent Flags
  3. Subcommands and Command Groups
  4. Distributing CLI Tools
← Back to Go Academy