Different structs in golang yaml

agolangf · · 859 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi,</p> <p>I&#39;ve got a yaml file that will look something like this:</p> <pre><code>--- hello: world something: [{&#39;values&#39;: { structB }, &#39;type&#39;: &#39;foo&#39;}, {&#39;type&#39;: &#39;bar&#39;, &#39;values&#39;: {.structB..}] </code></pre> <p>the &#39;something&#39; 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&#39;s only one static type:</p> <pre><code>type graphconfig struct { Graph string `yaml:&#34;graph&#34;` Types [] XXX Thing struct{ C int D []int } } </code></pre> <p>But in the above case, I don&#39;t know how to express the &#34;Types&#34; element, so that it can be different things.</p> <p>Thanks in advance!</p> <hr/>**评论:**<br/><br/>IntellectualReserve: <pre><p>If I&#39;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&#39;t. It&#39;ll map the values to map[string]interface{}.</p> <p>Here&#39;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

859 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传