Go 自定义解组器,如何在自定义类型上实现解组器接口

Go 自定义解组器,如何在自定义类型上实现解组器接口,go,yaml,unmarshalling,custom-type,Go,Yaml,Unmarshalling,Custom Type,我解析一个.yaml文件,并需要以自定义方式解组它的一个属性。我使用的是“gopkg.in/yaml.v2”包 所涉及的属性存储在my.yaml文件中,如下所示: endPointNumberSequences: AD1: [ 0, 10, 14, 1, 11, 2, 100, 101, 12 ] 因此它基本上是一种map[string][]uint16类型。 但是我需要map[string]EpnSeq其中EpnSeq被定义为: 键入EpnSeq映射[uint16]uint16 我的结构

我解析一个.yaml文件,并需要以自定义方式解组它的一个属性。我使用的是
“gopkg.in/yaml.v2”

所涉及的属性存储在my.yaml文件中,如下所示:

endPointNumberSequences:
  AD1: [ 0, 10, 14, 1, 11, 2, 100, 101, 12 ]
因此它基本上是一种
map[string][]uint16
类型。
但是我需要
map[string]EpnSeq
其中
EpnSeq
被定义为:
键入EpnSeq映射[uint16]uint16

我的结构:

type CitConfig struct {
    // lots of other properties
    // ...

    EndPointNumberSequences  map[string]EpnSeq `yaml:"endPointNumberSequences"`
}
我尝试在其上实现解组器接口,如下所示:

// 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
}
我的问题是在
unmarshalyml
函数中未定义
EpnSeq
类型,导致运行时出现零指针异常。

我如何在这里正确实现解组器接口?

既然@Volker没有将他的评论作为答案发布,为了完整起见,我会这样做。
因此,我已经走上了正确的道路,但在初始化结构时未能解除对其指针接收器的引用:

// 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
    }

    // make sure to dereference before assignment, 
    // otherwise only the local variable will be overwritten
    // and not the value the pointer actually points to
    *e = make(EpnSeq, len(yamlEpnSequence))
    for priority, epn := range yamlEpnSequence {
        e[epn] = uint16(priority) // no crash anymore
    }

    return nil
}

在写入之前制作
EpnSeq?例如,
*E=make(EpnSeq,len(yamlpensequence))
。需要一个指针接收器。哇,我只是傻了。我尝试了这一点,但在使用make()赋值之前,我未能首先解除对指针的引用。这样,指针只在本地更改。。。我的错,sorry@Volker:写一个答案,给自己弄点网络积分,呜呼;)