Golang flag.DurationVar() function example

package flag

DurationVar defines a time.Duration flag with specified name (2nd parameter), default value(3rd parameter), and usage string(4th parameter). The argument p(1st parameter) points to a time.Duration variable in which to store the value of the flag.

Golang flag.DurationVar() function usage example

 package main

 import (
 "flag"
 "fmt"
 "time"
 )

 var (
 naptime = time.Millisecond * 250
 nap time.Duration
 )

 func init() {
 flag.DurationVar(&nap, "poll", naptime, "Time to wait between polling for changes")
 }

 func main() {
 fmt.Println(flag.Lookup("poll")) // print Flag struct

 fmt.Println(nap)
 }

Output :

&{poll Time to wait between polling for changes 250ms 250ms}

250ms

Reference :

http://golang.org/pkg/flag/#DurationVar

Advertisement