<pre><code>package main
import (
"encoding/json"
"reflect"
)
func main() {
t, _ := json.Marshal(20)
println(string(t))
println(reflect.TypeOf(t).String())
println(len(t))
for _, b := range(t) {
println(b)
}
}
</code></pre>
<p>The output on "go run" is:
20
[]uint8
2
50
48</p>
<p>Where did 50 & 48 come from?</p>
<hr/>**评论:**<br/><br/>singular_pirate: <pre><p>50 and 48 are the values of the codepoints of '2' and '0'.</p>
<p>Example: <a href="https://play.golang.org/p/LgyuvYzKW2" rel="nofollow">https://play.golang.org/p/LgyuvYzKW2</a></p>
<p>Reference: <a href="https://blog.golang.org/strings" rel="nofollow">https://blog.golang.org/strings</a></p></pre>ikickrobots: <pre><p>Ok. Thank you.</p></pre>natefinch: <pre><p>protip: never drop the error, even in examples. I can't tell you how many times I've done that and regretted it when I did something dumb. Just throw a <code>if err != nil { log.Fatal(err) }</code> in there for kicks. It'll save you a headache one day.</p></pre>dilap: <pre><p>this is good advice.</p>
<p>if you want to make it slightly less of a pain for throwaway scripts, just have a function like</p>
<pre><code>func check(err error) {
if(err != nil) {
log.Fatal(err)
}
}
</code></pre>
<p>so then you just need to insert <code>check(err)</code> in your code, which is close to painless.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传