Default for `time.Duration` not working when using units
wonderhoss opened this issue · comments
Philippa Taprogge commented
For fields of type time.Duration
the documentation suggests that a default of "1m" should be parsed into the appropriate duration (i.e. 1 * time.Minute
).
This does not seem to be working.
Defaults are only correctly applied when using numbers only (which are then interpreted as nanoseconds:
import (
"fmt"
"time"
"github.com/mcuadros/go-defaults"
)
type Foo struct {
ConnectionTimeout time.Duration `default:"2000000000"`
}
func main() {
c := &Foo{}
defaults.SetDefaults(c)
fmt.Printf("Foo: %+v", c)
}
Output:
Foo: &{ConnectionTimeout:2s}
However:
import (
"fmt"
"time"
"github.com/mcuadros/go-defaults"
)
type Foo struct {
ConnectionTimeout time.Duration `default:"20s"`
}
func main() {
c := &Foo{}
defaults.SetDefaults(c)
fmt.Printf("Foo: %+v", c)
}
Output:
Foo: &{ConnectionTimeout:0s}
Máximo Cuadros commented