Created
March 10, 2016 14:50
-
-
Save jatinn/b2eb8f4878c18573e6a7 to your computer and use it in GitHub Desktop.
Parse time.Duration with gcfg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
⧰ go run duration.go | |
10s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
⧰ 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