// The Unmarshaler interface may be implemented by types to customize their // behavior when being unmarshaled from a YAML document. type Unmarshaler interface { UnmarshalYAML(value *Node) error }
序列化
至于序列化为 yaml 文档,也很简单:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// // write to stdout // // https://github.com/go-yaml/yaml/issues/166 // https://github.com/go-yaml/yaml/pull/455 var buffer bytes.Buffer encoder := yaml.NewEncoder(&buffer) encoder.SetIndent(2) err = encoder.Encode(raw) if err != nil { return errors.Wrapf(err, "failed to encode config: %s", err) }
// The Marshaler interface may be implemented by types to customize their // behavior when being marshaled into a YAML document. The returned value // is marshaled in place of the original value implementing Marshaler. // // If an error is returned by MarshalYAML, the marshaling procedure stops // and returns with the provided error. type Marshaler interface { MarshalYAML() (interface{}, error) }