SoFunction
Updated on 2025-03-05

In-depth analysis of the standard library flag in golang

The built-in flag package in Go language implements the parsing of command line parameters, and the flag package makes it easier to develop command line tools.

If you just want to get command line parameters, you can use it like the following code example to get command line parameters.

func main() {
	// Get command line parameters	// :[]string
	if len() > 0 {
		for i, v := range  {
			(i, v)
		}
	}
}

Execute the command:go run .\ host:127.0.0.1 port:8080
Output result:

 C:\Users\mayanan\AppData\Local\Temp\go-build3549800423\b001\exe\
 host:127.0.0.1

 port:8080

Is a string slice that stores command line parameters, and its first element is the name of the execution file.

Basic use of flag package

Parameter Type

The command line parameter types supported by the flag package include bool, int, int64, uint, uint64, float float64, string, and duration.

flag parameter Valid value
String flag Legal string
integer flag Types such as 1234, 0664, 0x1234 can also be negative numbers.
Floating point flag Legal floating point number
bool type flag 1, 0, t, f, T, F, true, false, TRUE, FALSE, True, False。
Time period flag Any legal time period string. Such as "300ms", "-1.5h", "2h45m". Legal units include "ns", "us" / "µs", "ms", "s", "m", "h".

Define command line flag parameters

()

The basic format is as follows:
(flag name, default value, help information)*TypeFor example, we want to define three command line parameters: name, age, and marriage. We can define them as follows:

func main() {
	// Use of ()	name := ("name", "Zhang San", "Name")
	age := ("age", 18, "age")
	married := ("married", false, "Marriage")
	delay := ("d", 0, "Time interval")

	()

	(*name, *age, *married, *delay)
}

Terminal input:
go run .\ -name lisi --age 88 -married=true --d=15s

Output:
lisi 88 true 15s

()

The basic format is as follows: (Type pointer, flag name, default value, help information) For example, we want to define three command line parameters: name, age, and marriage. We can define them as follows:

func main() {
	var name string
	var age uint
	var married bool
	var d 

	(&name, "name", "Wang Wu", "Name")
	(&age, "age", 18, "age")
	(&married, "m", false, "Marriage")
	(&d, "duration", 0, "Time interval")

	()

	(name, age, married, d)

}

enter:
go run .\ -name lisi --age 35 -m=true --duration=1h15m36s

Output:
lisi 35 true 1h15m36s

()

After defining the command line flag parameters through the above two methods, you need to call () to parse the command line parameters.

The following command line parameter formats are supported:

-flag xxx (using spaces, a - symbol)
--flag xxx (using spaces, two - symbols)
-flag=xxx (using the equal sign, a - symbol)
--flag=xxx (using equal sign, two - symbols)
Among them, Boolean parameters must be specified in the same way as equals.

Flag parsing stops before the first non-flag parameter (single "-"not flag parameter), or after the terminator "-".

flag other functions

  • () ////Return other parameters after the command line parameter, with []string type
  • () //Return the number of other parameters after the command line parameter
  • () //Return the number of command line parameters used

Complete example

func main() {
	var name string
	var age uint
	var married bool
	var d 

	(&name, "name", "Wang Wu", "Name")
	(&age, "age", 18, "age")
	(&married, "m", false, "Marriage")
	(&d, "duration", 0, "Time interval")

	()

	(name, age, married, d)  // lisi 35 true 1h15m36s

	(())  // [abc true 123]
	(())  // 3
	(())  //  4

}

enter:
go run .\ -name lisi --age 35 -m=true --duration=1h15m36s abc true 123

Output:

lisi 35 true 1h15m36s
[abc true 123]
3
4

This is all about this article about the standard library flag in golang. For more relevant go standard library flag content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!