【问题标题】:how to preserve spaces and newline in golang yaml parser?如何在 golang yaml 解析器中保留空格和换行符?
【发布时间】:2015-02-09 06:15:16
【问题描述】:

我想要类似的东西

这里有一些文字,
    此处缩进文本
    下一个缩进的 texr 在这里

我试过这种 yaml 样式

键: | 这里有一些文字, 此处缩进文本 下一个缩进文本在这里

上面的 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
}

【问题讨论】:

  • 请向我们展示您的 Go 代码如何解析 yaml 文件。

标签: parsing go yaml


【解决方案1】:

我不确定您使用哪个解析器来解析 YAML,但这里有一个运行良好的 sn-p,我使用了 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"))
}

【讨论】:

    猜你喜欢
    • 2020-09-15
    • 1970-01-01
    • 2019-03-17
    • 2011-03-24
    • 2018-02-14
    • 1970-01-01
    • 2023-04-06
    • 2014-05-17
    • 2011-05-25
    相关资源
    最近更新 更多