Flags and Arguments
Go provides support for creating command-line interface tools. Many times, when we are writing Go programs that are executables, they need to accept various inputs. These inputs could include the location of a file, a value to run the program in the debug state, getting help to run the program, and more. All of this is made possible by a package in the Go standard library called flag
. It is used to allow the passing of arguments to the program. A flag is an argument that is passed to a Go program. The order of the flags being passed to the Go program using the flag
package does not matter to Go.
To define your flag
, you must know the flag
type you will be accepting. The flag
package provides many functions for defining flags. Here is a sample list:
func Bool(name string, value bool, usage string) *bool func Duration(name string, value time.Duration, usage string) *time.Duration func Float64(name string, value float64, usage string) *float64 func Int(name string...