外部软件包的自定义golang JSON封送处理

Ala*_*era 1 go

如何从外部包向结构添加自定义JSON封送处理?我无法将MarshalJSON和添加UnmarshalYAML到这些结构中。

Ale*_*mov 5

您可以将该结构添加到您定义的结构中。然后添加功能MarshalJSON,并UnmarshalYAML给你的结构:

type YourStruct struct {
    ImportedStruct
}

func (s *YourStruct) MarshalJSON() ([]byte, error) {
    ...
}
Run Code Online (Sandbox Code Playgroud)