Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected line break after marshal #348

Closed
kopwei opened this issue Mar 22, 2018 · 1 comment
Closed

Unexpected line break after marshal #348

kopwei opened this issue Mar 22, 2018 · 1 comment

Comments

@kopwei
Copy link

kopwei commented Mar 22, 2018

When I want to marshal a map value into a yaml text file, i found that Mashal() function generates an unexpected line break in the mashalled bytes array.
Here is the example code:

package main

import (
    "fmt"
    "gopkg.in/yaml.v2"
)

func main() {
    m := make(map[string][]string)
    var values []string
    values = append(values, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbb")
    m["key"] = values
    converted_str, _ := yaml.Marshal(&m)
    fmt.Printf("%v\n", converted_str)
    fmt.Printf("%s\n", converted_str)
}
Output:
$ go run test.go 
[107 101 121 58 10 45 32 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 10 32 32 98 98 98 10]
key:
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  bbb

There is an extra line break generated before "bbb". If I reduce the number of character 'a', Marshal() function works as expected and no extra line break.

Go version: 1.10
Platform: MacOS 10.13.3 and Ubuntu 16.04.4

@kopwei
Copy link
Author

kopwei commented Mar 22, 2018

Found the cause, there is a "emitter.best_width" value in source code and it is set to 80 and will generate line break if the space character's index is larger than that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant