prettyprinter or print_r for Golang?

blov · · 945 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Is there a functional equivalent to Python&#39;s pprint or PHP&#39;s print_r in golang? Specifically a function that can dump out most basic variable types as text you can take a look at without having to handle (for example) iteration logic in arrays/maps/slices/sets/etc.</p> <p>edit: <a href="/u/jerf" rel="nofollow">/u/jerf</a> says:</p> <pre><code>fmt.Printf(&#34;%#v\n&#34;, obj) is the basic one I use a lot of. It doesn&#39;t recurse infinitely, though. For that you can pick up a pretty printer from github. </code></pre> <p>Works for my purposes, will have to see if the base libraries have a prettyprinter in them somewhere.</p> <p>edit 2, electric boogaloo: <a href="/u/raff99" rel="nofollow">/u/raff99</a> also has a really good suggestion (that ends up more like pprint)</p> <pre><code>For some cases you can print out a JSON representation of your objects. Write your own utility function or use something like this: http://godoc.org/github.com/gobs/simplejson#DumpString For something more fancy you can use this: https://github.com/gobs/pretty (there is a PrettyPrint for printing and a PrettyFormat for passing the formatted string around) </code></pre> <p>Looks very pretty if you have the json pretty something or other plugin installed in chrome.</p> <hr/>**评论:**<br/><br/>jerf: <pre><p><code>fmt.Printf(&#34;%#v\n&#34;, obj)</code> is the basic one I use a lot of. It doesn&#39;t recurse infinitely, though. For that you can pick up a pretty printer from github.</p></pre>itripovermyownfeet: <pre><p>Does that work with fmt.Fprintf, my golang isn&#39;t so good as of yet. I can print to the console but I like to spit this back through an http.ResponseWriter.</p> <p>edit: It does, neat! Thanks mang.</p></pre>jerf: <pre><p>Yes, also fmt.Sprintf.</p> <p>Some others may pop up with some other suggestions but I&#39;m pretty sure the base library does not have a &#34;full&#34; pretty printer, as in, I&#39;ve been over the base libraries a couple of times and will actually be surprised if someone says there is one.</p></pre>raff99: <pre><p>For some cases you can print out a JSON representation of your objects. Write your own utility function or use something like this: <a href="http://godoc.org/github.com/gobs/simplejson#DumpString" rel="nofollow">http://godoc.org/github.com/gobs/simplejson#DumpString</a></p> <p>For something more fancy you can use this: <a href="https://github.com/gobs/pretty" rel="nofollow">https://github.com/gobs/pretty</a></p> <p>(there is a PrettyPrint for printing and a PrettyFormat for passing the formatted string around)</p></pre>RandNho: <pre><p>I use <a href="https://github.com/davecgh/go-spew" rel="nofollow">https://github.com/davecgh/go-spew</a> for printing deep object stuff for debugging purposes. And to look at them.</p></pre>bonekeeper: <pre><p>I second this, Spew has been good for me so far.</p></pre>itripovermyownfeet: <pre><p>Spew was another recommendation I saw elsewhere.</p></pre>

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

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