2015-02-09 2 views
0

Я хочу что-то вродеКак сохранить пробелы и новую строку в golang yaml parser?

Some text here, 
    indented text here 
    next indented texr here

Я попробовал этот YAML стиль

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

выше код YAML сохраняет только символ новой строки, но отбрасывает отступом пространства. Как сохранить эти лишние пробелы?

код, который я использовал для разбора файла YAML 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 
} 

+0

Пожалуйста, покажите нам свой код Go, как вы разобрать файл YAML. – icza

ответ

0

Я не уверен, какой парсер вы используете для разбора YAML, но вот фрагмент, который работает довольно хорошо, я использовал 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")) 
}