MCPcopy Create free account
hub / github.com/caarlos0/env / ExampleParse_customTimeFormat

Function ExampleParse_customTimeFormat

example_test.go:211–230  ·  view source on GitHub ↗

By default, env supports anything that implements the `TextUnmarshaler` interface, which includes `time.Time`. The upside is that depending on the format you need, you don't need to change anything. The downside is that if you do need time in another format, you'll need to create your own type and

()

Source from the content-addressed store, hash-verified

209// The downside is that if you do need time in another format, you'll need to
210// create your own type and implement `TextUnmarshaler`.
211func ExampleParse_customTimeFormat() {
212 // type MyTime time.Time
213 //
214 // func (t *MyTime) UnmarshalText(text []byte) error {
215 // tt, err := time.Parse("2006-01-02", string(text))
216 // *t = MyTime(tt)
217 // return err
218 // }
219
220 type Config struct {
221 SomeTime MyTime `env:"SOME_TIME"`
222 }
223 os.Setenv("SOME_TIME", "2021-05-06")
224 var cfg Config
225 if err := Parse(&cfg); err != nil {
226 fmt.Println(err)
227 }
228 fmt.Print(cfg.SomeTime)
229 // Output: {0 63755856000 <nil>}
230}
231
232// Parse using extra options.
233func ExampleParseWithOptions_customTypes() {

Callers

nothing calls this directly

Calls 1

ParseFunction · 0.85

Tested by

no test coverage detected