Golang flag.Duration() function example

package flag

Duration defines a time.Duration flag with specified name, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the flag.

Golang flag.Duration() function usage example

 package main

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

 var buildTimeout = flag.Duration("buildTimeout", 60*time.Minute, "Maximum time to wait for builds and tests")

 func runTimeout(timeout time.Duration) {
 fmt.Println(timeout)
 }

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

 runTimeout(*buildTimeout)
 }

Output :

&{buildTimeout Maximum time to wait for builds and tests 1h0m0s 1h0m0s}

1h0m0s

Reference :

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

Advertisement