2015-02-09 187 views
0

我想是这样如何在golang yaml解析器中保留空格和换行符?

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 2015-02-09 07:21:21

回答

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")) 
}