PSA: when json.Marshal-ling (a slice of) Struct

agolangf · · 590 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>make sure the attributes are capitalized or they won&#39;t be published... I understand that this is a convention of the language, but I failed to understand it early enough today! My output (cgo, ffi, ruby) would simply come back as a slice of empty maps :)</p> <pre><code>// good type WeightedWord struct { Text string Weight int } //bad type WeightedWord struct { text string weight int } </code></pre> <hr/>**评论:**<br/><br/>jamra06: <pre><pre><code>type WeightedWord struct { Text string `json:&#34;text&#34;` Weight int `json:&#34;weight&#34;` } </code></pre> <p>This will get you what you were looking for originally.</p></pre>mekdigital: <pre><p>that&#39;s a neat trick learn! thanks</p></pre>XANi_: <pre><p>RTFM, there are few more useful options like <code>omitempty</code></p></pre>mekdigital: <pre><p>I need to tag my post with &#34;SUPA-NOOB&#34; :D</p></pre>XANi_: <pre><p>golang has REALLY nice doc tools, you can point your browser to <a href="https://godoc.org/any/webpage/hosting/repo" rel="nofollow">https://godoc.org/any/webpage/hosting/repo</a> and get a doc for package</p> <p>It also have very nice feature of &#34;verifiable examples&#34; where (when you name and document it correctly) example code is ran during <code>go test</code> so it is very hard to accidentally break example code when changing other parts of your codebase</p></pre>mekdigital: <pre><p>OH NO MORE THINGS TO LEARN ®</p></pre>XANi_: <pre><p>It never ends</p></pre>elusivegopher: <pre><p>Really? RTFM just because he doesn&#39;t know about struct tags?</p> <p>Seems a bit harsh for one of today&#39;s <a href="https://xkcd.com/1053/" rel="nofollow">10,000</a>.</p></pre>XANi_: <pre><p>What is wrong with reading the fine manual ?</p> <p>I&#39;ve found out few useful things in json module thanks to that, for example that you can tell it to parse only &#34;first&#34; level of json into <code>map[string]json.RawMessage</code> which is very handy if you have to interact with <a href="https://doc.powerdns.com/md/authoritative/backend-remote/" rel="nofollow">certain types of api</a></p> <blockquote> <p>Seems a bit harsh for one of today&#39;s 10,000.</p> </blockquote> <p>That XKCD is ...not relevant in any way.</p> <p>If you use API and dont bother to even gloss over documentation you will eventually hurt yourself, or everyone involved</p></pre>xkcd_transcriber: <pre><p><a href="http://imgs.xkcd.com/comics/ten_thousand.png" rel="nofollow">Image</a></p> <p><a href="http://m.xkcd.com/1053/" rel="nofollow">Mobile</a></p> <p><strong>Title:</strong> Ten Thousand</p> <p><strong>Title-text:</strong> Saying &#39;what kind of an idiot doesn&#39;t know about the Yellowstone supervolcano&#39; is so much more boring than telling someone about the Yellowstone supervolcano for the first time.</p> <p><a href="http://www.explainxkcd.com/wiki/index.php/1053#Explanation" rel="nofollow">Comic Explanation</a></p> <p><strong>Stats:</strong> This comic has been referenced 6140 times, representing 6.2372% of referenced xkcds.</p> <hr/> <p><sup><a href="http://www.xkcd.com" rel="nofollow">xkcd.com</a></sup> <sup>|</sup> <sup><a href="http://www.reddit.com/r/xkcd/" rel="nofollow">xkcd sub</a></sup> <sup>|</sup> <sup><a href="http://www.reddit.com/r/xkcd_transcriber/" rel="nofollow">Problems/Bugs?</a></sup> <sup>|</sup> <sup><a href="http://xkcdref.info/statistics/" rel="nofollow">Statistics</a></sup> <sup>|</sup> <sup><a href="http://reddit.com/message/compose/?to=xkcd_transcriber&amp;subject=ignore%20me&amp;message=ignore%20me" rel="nofollow">Stop Replying</a></sup> <sup>|</sup> <sup><a href="http://reddit.com/message/compose/?to=xkcd_transcriber&amp;subject=delete&amp;message=delete%20t1_cznisgk" rel="nofollow">Delete</a></sup></p></pre>

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

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