我想创建 structs = 每种类型的命令。
命令具有 xml 的公共部分 - CommandResult。我创建了接口命令。我需要SomeCommand实现Command并且可以解析为xml,而且CommandResult中必须实现IsError,其他功能必须通过SomeCommand来实现。
代码:
type Command interface {
IsError() bool
Request(buf *bufio.Writer, params interface{}) error
...
}
// Result of request
type CommandResult struct {
Code int `xml:"code,attr" json:"code"`
Message string `xml:"msg" json:"msg"`
}
// this Command's func is realized by CommandResult
func (self CommandResult) IsError() bool {
return true
}
// some command
type SomeCommand struct {
CommandResult // CommandResult `xml:"response>result" json:"result"`
}
// this Command's func is realized by SomeCommand
func (self SomeCommand) Request(buf *bufio.Writer, params interface{}) error {
return nil
}
// other Command's functions are realized by CommandResult too
XML:
<epp>
<response>
<result code="1000">
<msg>Command completed successfully</msg>
</result>
<trID>
<svTRID>asd</svTRID>
</trID>
</response>
</epp>
预期结果:
a := SomeCommand
xml.NewDecoder(reader).Decode(&a)
// a.CommandResult.Code = 1000
// a.CommandResult.Message = 'Command completed successfully'
// a implements Command
拉风的咖菲猫
相关分类