我有一个以字节数组作为字段的结构。这是代码:
package main
import (
"fmt"
"gopkg.in/yaml.v3"
)
type A struct {
PublicKey []byte `json:"PublicKey" yaml:"PublicKey"`
}
// Implements the Marshaler interface of the yaml pkg.
func (a A) MarshalYAML() (interface{}, error) {
type alias A
node := yaml.Node{}
_ = node.Encode(alias(a))
return node, nil
}
func PublicKey() {
token := []byte{87, 88, 89, 90}
a := A{PublicKey: token}
fmt.Printf("A: %+v\nA.PublicKey:%s\n\n", a, a.PublicKey)
out, _ := yaml.Marshal(a)
fmt.Println(string(out))
}
func main() {
PublicKey()
}
这是输出:
A: {PublicKey:[87 88 89 90]}
A.PublicKey:WXYZ
PublicKey:
- 87
- 88
- 89
- 90
是否可以让 marshal-er 将其输出为字符串而不是字节数组?例如:
PublicKey: WXYZ
慕桂英3389331
相关分类