<p>Hi,</p>
<p>I've got a yaml file that will look something like this:</p>
<pre><code>---
hello: world
something: [{'values': { structB }, 'type': 'foo'}, {'type': 'bar', 'values': {.structB..}]
</code></pre>
<p>the 'something' key is a list, which has a top level dictionary while has a type key... Based on that struct, the values key will point to different structs.</p>
<p>How do you map to the different structs in golang? I know how to do it if there's only one static type:</p>
<pre><code>type graphconfig struct {
Graph string `yaml:"graph"`
Types [] XXX
Thing struct{
C int
D []int
}
}
</code></pre>
<p>But in the above case, I don't know how to express the "Types" element, so that it can be different things.</p>
<p>Thanks in advance!</p>
<hr/>**评论:**<br/><br/>IntellectualReserve: <pre><p>If I'm understanding your question correctly, you probably want something like</p>
<pre><code>[]map[string]interface{}
</code></pre></pre>purpleidea: <pre><p>But how will the yaml lib know which struct to put there so that when you decode it, the data is stored in the struct that matches the correct pattern??</p></pre>IntellectualReserve: <pre><p>It won't. It'll map the values to map[string]interface{}.</p>
<p>Here's a stack overflow on a related topic: <a href="http://stackoverflow.com/questions/26290485/golang-yaml-reading-with-map-of-maps" rel="nofollow">http://stackoverflow.com/questions/26290485/golang-yaml-reading-with-map-of-maps</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传