Standard library / flag

flag.FlagSet.Duration()

Registers a flag on an independent FlagSet. 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 (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration

Parameters & Attributes

NameTypeDescription
namestringName of the flag or flag set.
valuetime.DurationDefault value for registration, or text to parse for Set.
usagestringHelp text describing the flag.

Return Value

*time.Duration

Registers a flag on an independent FlagSet.

Working Example

Example Snippetgo
package main
import (  "flag"  "fmt"  "os"  "time")
func check(e error) {  if e != nil {    panic(e)  }}
func main() {  f := flag.NewFlagSet("demo", flag.ContinueOnError)  v := f.Duration("x", time.Second, "value")  check(f.Parse(os.Args[1:]))  fmt.Println(*v)}
Go Reference