Flags and Persistent Flags
String, int, bool flags and Viper integration
Flags and Persistent Flags is a free Go Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.
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 bBinding 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
Learn Go with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Flags and Persistent Flags” lesson free?
Yes — any 3 lessons of the Go Academy learning path, including “Flags and Persistent Flags”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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
- Cobra Fundamentals
- Flags and Persistent Flags
- Subcommands and Command Groups
- Distributing CLI Tools