Standard library / flag
flag.DurationVar()
Registers a flag on the default command line. Binds existing time.Duration storage rather than returning a pointer. Duration parsed using time.ParseDuration; include a unit such as ms or s. Run with arguments: ["-x=250ms"]. Expected stdout: '250ms\n'.
Formal Syntax & Structure
go syntaxgo
func DurationVar(p *time.Duration, name string, value time.Duration, usage string)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| p | *time.Duration | Pointer to storage updated during parsing. |
| name | string | Name of the flag or flag set. |
| value | time.Duration | Default value for registration, or text to parse for Set. |
| usage | string | Help text describing the flag. |
Working Example
Example Snippetgo
package main
import ( "flag" "fmt" "time")
func main() { var v time.Duration flag.DurationVar(&v, "x", time.Second, "value") flag.Parse() fmt.Println(v)}