【发布时间】:2018-09-06 21:30:22
【问题描述】:
我解析一个 .yaml 文件,需要以自定义方式解组其中一个属性。我正在使用"gopkg.in/yaml.v2" 包。
有问题的属性是这样存储在我的 .yaml 文件中的:
endPointNumberSequences:
AD1: [ 0, 10, 14, 1, 11, 2, 100, 101, 12 ]
所以它基本上是一种map[string][]uint16 类型。
但我需要map[string]EpnSeq,其中EpnSeq 定义为:type EpnSeq map[uint16]uint16
我的结构:
type CitConfig struct {
// lots of other properties
// ...
EndPointNumberSequences map[string]EpnSeq `yaml:"endPointNumberSequences"`
}
我尝试在其上实现 Unmarshaler 接口,如下所示:
// Implements the Unmarshaler interface of the yaml pkg.
func (e EpnSeq) UnmarshalYAML(unmarshal func(interface{}) error) error {
yamlEpnSequence := make([]uint16, 0)
err := unmarshal(&yamlEpnSequence)
if err != nil {
return err
}
for priority, epn := range yamlEpnSequence {
e[epn] = uint16(priority) // crashes with nil pointer
}
return nil
}
我的问题是 UnmarshalYAML 函数内部没有定义 EpnSeq 类型,导致运行时出现 nil 指针异常。
如何在此处正确实现 Unmarshaler 接口?
【问题讨论】:
-
makeEpnSeq 在写入之前?例如。*e = make(EpnSeq, len(yamlEpnSequence))。无论如何都需要一个指针接收器。 -
哇,我只是傻了。我试过这个,但在使用 make() 分配之前,我没有先取消引用指针。这样,指针仅在本地更改...我的错,对不起
-
@Volker:写一个答案,让自己获得一些互联网积分,哇哦 ;)
标签: go yaml unmarshalling custom-type