<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
'0': Heartbeat
'1': Test Request
'2': Resend Request
'3': Reject
'4': Sequence Reset
'5': Logout
'8': Execution Report
S: Quote
AE: Trade Capture Report
B: News
h: Trading Session Status
f: Security Status
</code></pre>
<p>I'm trying to unmarshal it with(using gopkg.in/yaml.v2):</p>
<pre><code>translationVal := map[string]string{}
err := yaml.Unmarshal([]byte(val), &translationVal)
</code></pre>
<p>However, I'm getting:</p>
<pre><code>line 1: cannot unmarshal !!str 'A' 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'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'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'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't know how. :)</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传