Figuring out the right struct to parse a simple YAML file in Golang

polaris · · 450 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have a fairly simple YAML document to parse into a (preferably) map in Go.</p> <p>YAML doc:</p> <pre><code>--- A: Logon &#39;0&#39;: Heartbeat &#39;1&#39;: Test Request &#39;2&#39;: Resend Request &#39;3&#39;: Reject &#39;4&#39;: Sequence Reset &#39;5&#39;: Logout &#39;8&#39;: Execution Report S: Quote AE: Trade Capture Report B: News h: Trading Session Status f: Security Status </code></pre> <p>I&#39;m trying to unmarshal it with(using gopkg.in/yaml.v2):</p> <pre><code>translationVal := map[string]string{} err := yaml.Unmarshal([]byte(val), &amp;translationVal) </code></pre> <p>However, I&#39;m getting:</p> <pre><code>line 1: cannot unmarshal !!str &#39;A&#39; into map[string]string </code></pre> <hr/>**评论:**<br/><br/>thatdude42: <pre><p>Checkout <a href="https://github.com/spf13/viper/blob/master/README.md" rel="nofollow">github.com/spf13/viper</a></p></pre>Hexodam: <pre><p>awesome library</p></pre>JUNneiluj: <pre><p>I&#39;m not sure if it fits your requirements, but this is what I use to unmarshal YAML: <a href="https://github.com/gulien/orbit/blob/master/context/context.go#L118" rel="nofollow">https://github.com/gulien/orbit/blob/master/context/context.go#L118</a></p> <p>In my case, I have no idea of the structure of the YAML document, so I&#39;m simply using an interface{}. You might also try:</p> <p><code>map[interface{}]interface{}</code></p></pre>intellion: <pre><p>it is solved, thank you!</p></pre>jerf: <pre><p>It&#39;s helpful to the nets to be sure to post <em>how</em> you solved it, so future searchers can find your solution and not merely be teased with the fact that it was solved but they still don&#39;t know how. :)</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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