栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

解析地图Yaml错误

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解析地图Yaml错误

您提供的Yaml令牌中包含错误。在此处验证代码中使用的Yaml https://prebeautify.org/yaml-
validator/cbaabb32

之后,创建结构类型的变量结果而不是数组。因为您使用的Yaml正在使用Runners数组和api_version创建一个结构。

这个

var runners []Result

应该

var runners Result

因为因为struct不是切片。为yaml中使用的功能名称获取命令列表。您需要遍历runners数组以查找函数名称并获取该函数的命令值。下面是工作代码:

package mainimport (    "fmt"    "log"    "gopkg.in/yaml.v2")var runContent = []byte(`api_ver: 1runners:  - name: function1    type:    - command: spawn child process    - command: build    - command: gulp  - name: function2    type:    - command: run function 1    - name: function3    type:    - command: ruby build  - name: function4    type:  - command: go build`)type Result struct {    Version string    `yaml:"api_ver"`    Runners []Runners `yaml:"runners"`}type Runners struct {    Name string    `yaml:"name"`    Type []Command `yaml:"type"`}type Command struct {    Command string `yaml:"command"`}func main() {    var runners Result    // parse mta yaml    err := yaml.Unmarshal(runContent, &runners)    if err != nil {        log.Fatalf("Error : %v", err)    }    commandList := getCommandList("function1", runners.Runners)    fmt.Printf("%+v", commandList)}func getCommandList(name string, runners []Runners) []Command {    var commandList []Command    for _, value := range runners {        if value.Name == name { commandList = value.Type        }    }    return commandList}

操场上的例子



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406505.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号