Skip to content

Instantly share code, notes, and snippets.

@jatinn
Created March 10, 2016 14:50
Show Gist options
  • Save jatinn/b2eb8f4878c18573e6a7 to your computer and use it in GitHub Desktop.
Save jatinn/b2eb8f4878c18573e6a7 to your computer and use it in GitHub Desktop.
Parse time.Duration with gcfg
package main
import (
"fmt"
"log"
"time"
"code.google.com/p/gcfg"
)
type duration struct {
time.Duration
}
func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
func main() {
cfgStr := `; Comment line
[section]
interval=10s # comment`
cfg := struct {
Section struct {
Interval duration
}
}{}
err := gcfg.ReadStringInto(&cfg, cfgStr)
if err != nil {
log.Fatalf("Failed to parse gcfg data: %s", err)
}
fmt.Println(cfg.Section.Interval.Duration)
}
⧰ go run duration.go
10s
⧰ go run duration.go
2016/03/10 09:48:26 Failed to parse gcfg data: time: unknown unit ss in duration 10ss
exit status 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment