go-yaml / yaml

YAML support for the Go language.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

excute home page code, the output space is 4

andyblog opened this issue · comments

When I use the sample code on the home page to execute, the output space is 4, but the output space on the home page is 2. What is the reason for this? Is there a place to configure the number of output spaces?

[root@VPS-BJ ~/test]#go run main.go 
--- t:
{Easy! {2 [3 4]}}

--- t dump:
a: Easy!
b:
    c: 2
    d: [3, 4]


--- m:
map[a:Easy! b:map[c:2 d:[3 4]]]

--- m dump:
a: Easy!
b:
    c: 2
    d:
        - 3
        - 4


[root@VPS-BJ ~/test]#ls
go.mod  go.sum  main.go
[root@VPS-BJ ~/test]#cat go.mod 
module test

go 1.19

require gopkg.in/yaml.v3 v3.0.1
var b bytes.Buffer
encoder := yaml.NewEncoder(&b)
encoder.SetIndent(2)
err = encoder.Encode(&m)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- m dump:\n%s\n\n", b.String())

So SetIndent can be used to set the indentation. But, regardless you would get this:

--- m dump:
a: Easy!
b:
  c: 2
  d:
    - 3
    - 4

and not this as it says in the README.md

--- m dump:
a: Easy!
b:
  c: 2
  d:
  - 3
  - 4