IhtkaS
IhtkaS

Reputation: 1424

how to preserve spaces and newline in golang yaml parser?

I want something like

Some text here,  
    indented text here
    next indented texr here

I tried this yaml style

key: |
    Some text here,
        indented text here
        next indented text here

the above yaml code preserves only the newline but discards the indented spaces. How to preserve those extra spaces?

code I used to parse the yaml file package main

import (
    "os"
    "fmt"
    "github.com/kylelemons/go-gypsy/yaml"
)

func main(){
    map_,err:=Parse()
fmt.Println(map_.Key("Key"),err)
}

func Parse() (yaml.Map, error) {
    file, err := os.Open("testindent.yaml")
        if err != nil {
        return nil, err
    }
    node, err := yaml.Parse(file)
        if err != nil {
            return nil, err
    }
    nodes := node.(yaml.Map)
    return nodes, nil
}

Upvotes: 3

Views: 2764

Answers (1)

Kishore
Kishore

Reputation: 9666

I am not sure which parser are you using to parse the YAML, but here is a snippet which works pretty good, I used viper.

testviber.yaml

invoice: 34843
date   : 2001-01-23
abc: |
   There once was a short man from Ealing
   Who got on a bus to Darjeeling
       It said on the door
       "Please don't spit on the floor"
   So he carefully spat on the ceiling
last_invoice: 34843

testviber.go

package main

import (
    "fmt"

    "github.com/spf13/viper"
)

func main() {
    viper.SetConfigName("testviber")
    viper.ReadInConfig()
    fmt.Printf("%v", viper.Get("abc"))
}

Upvotes: 2

Related Questions