简体   繁体   English

阵容来自Yaml - Golang

[英]Array from Yaml - Golang

I try to create configuration file for my go app what loops through some jobs. 我尝试为我的go app创建配置文件,循环执行某些作业。

My .yaml file looks like that (array): 我的.yaml文件看起来像那样(数组):

jobToRun:
  - name: This is my first job to run
    sqlToRun: select 1 from some_table
    someVariable: 1

  - name: Other job to run
    sqlToRun: select 2 from some_table
    someVariable: 2

I have successfully imported the YAML file and created also structure. 我已成功导入YAML文件并创建了结构。


    type Service struct {
        JobToRun []struct {
            Name         string `yaml:"name"`
            SQLToRun     string `yaml:"SqlToRun"`
            SomeVariable int    `yaml:"someVariable"`
        } `yaml:"jobToRun"`
    }

But I have no idea how to assign them to variable. 但我不知道如何将它们分配给变量。

I tried some stuff what work with Json array-s but without any luck. 我尝试了一些与Json array-s兼容但没有运气的东西。

So I tried to print it to console without any luck: 所以我试着将它打印到控制台而没有任何运气:

println(service.JobToRun.name[0])

before that I tried to assign that SQL to my variable (which works if it is not an array item. 在此之前,我尝试将该SQL分配给我的变量(如果它不是数组项,则可以使用它。

var sqlQuery = service.JobToRun.name[0] 

And here is what I try to accomplish: 这是我试图完成的事情:

I take the Job parameters from .yaml array and run it. 我从.yaml数组中获取Job参数并运行它。
I am using that kind of array in YAML because it is easiest way to add new jobs. 我在YAML中使用这种数组,因为它是添加新作业的最简单方法。

ah as soon as i posted it i facepalmed. 我张贴它的时候我就趴在脸上。


println(service.JobToRun[0].Name)

so the reason was that "JobToRun is an array not "Name" 所以原因是“JobToRun是一个数组而不是”名称“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM