【发布时间】:2018-08-09 19:33:02
【问题描述】:
我的结构如下:
type Connect struct {
ClientID string `yaml:"clientid"`
Password string `yaml:"password"`
Timeout time.Duration `yaml:"timeout"`
}
c1 := `
id: 'client1'
password: 'hhhhhhha'
timeout: 10
`
c2 := `
id: 'client2'
password: 'llllllla'
timeout: '10'
`
c3 := `
id: 'client3'
password: 'hhhhhhha'
timeout: 10s
`
c4 := `
id: 'client4'
password: 'llllllla'
timeout: '10s'
`
如上图,Timeout的类型是time.Duration,默认单位是纳秒,但是我想得到结果:c1 && c2有错误,c3 && c4有效(Timeout的配置必须有单位)。我应该如何为 yaml 重写 UnmarshalYAML() 方法?非常感谢。
【问题讨论】:
标签: go yaml unmarshalling