system clear in go

blov · · 1547 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey!Could anyone show me the equivalent in go to the system(&#34;clear&#34;); in C ? Thanks in advance</p> <hr/>**评论:**<br/><br/>TeranNotTerran: <pre><p>One way:</p> <pre><code>$ clear | ~/code/programtheplanet/hexout 1B5B333B4A1B5B481B5B324A $ cat clear.go package main import &#34;os&#34; func main() { os.Stdout.Write([]byte{0x1B, 0x5B, 0x33, 0x3B, 0x4A, 0x1B, 0x5B, 0x48, 0x1B, 0x5B, 0x32, 0x4A}) } </code></pre></pre>chipaca: <pre><p>or <code>os.Stdout.WriteString(&#34;\x1b[3;J\x1b[H\x1b[2J&#34;)</code></p></pre>TeranNotTerran: <pre><p>Another:</p> <pre><code>package main import ( &#34;os&#34; &#34;os/exec&#34; ) func main() { out, _ := exec.Command(&#34;/usr/local/bin/clear&#34;).Output() os.Stdout.Write(out) } </code></pre></pre>barsonme: <pre><p>Is this what you&#39;re looking for? </p> <p><a href="https://golang.org/pkg/os/exec/" rel="nofollow">https://golang.org/pkg/os/exec/</a></p></pre>seomis: <pre><p>exactly, thanks!</p></pre>esesci: <pre><p>won&#39;t work on windows fyi</p></pre>seomis: <pre><p>All you need in windows is</p> <p>package main</p> <p>import ( &#34;os&#34; &#34;os/exec&#34; )</p> <p>func main() { cmd := exec.Command(&#34;cmd&#34;, &#34;/c&#34;, &#34;cls&#34;) cmd.Stdout = os.Stdout cmd.Run() }</p> <p>almost the same!</p></pre>esesci: <pre><p>you won&#39;t get cross platform compatibility like this. </p></pre>seomis: <pre><p>well you can always use runtime.GOOS variable to identify the os</p> <p>if runtime.GOOS == &#34;windows&#34; { fmt.Println(&#34;Hello from Windows&#34;) }</p></pre>esesci: <pre><p>it&#39;s a hack still. using a lib instead (like ncurses etc) to handle a full screen app seems like a better idea. (with more overheas obviously )</p></pre>65a: <pre><p>it seems like you might be interested in ncurses bindings, where you can &#34;draw&#34; to the console. <a href="https://github.com/rthornton128/goncurses" rel="nofollow">https://github.com/rthornton128/goncurses</a><br/> There&#39;s also this:<br/> <a href="https://godoc.org/github.com/jroimartin/gocui" rel="nofollow">https://godoc.org/github.com/jroimartin/gocui</a> </p></pre>RandNho: <pre><p>Woudn&#39;t <a href="https://github.com/nsf/termbox-go" rel="nofollow">https://github.com/nsf/termbox-go</a> be enough?</p> <p>Wait, it returns to pre-draw state at exit. If I remember my experiments correctly.</p></pre>TeranNotTerran: <pre><p>That&#39;s a pretty heavy way to do it, but may be the most foolproof. Generally, a pretty common byte sequence will do the job on most consoles.</p></pre>65a: <pre><p>I agree. I did want to make sure OP was aware that console drawing abstractions exist, because if you&#39;re clearing the console, you&#39;re probably trying to update the screen. Ncurses will also deal with odd consoles better than a mostly works byte sequence.</p></pre>seomis: <pre><p>Useful, thanks! </p></pre>iqandjoke: <pre><p>Umm... <a href="http://stackoverflow.com/questions/31212546/systemclear-equivalent-in-go" rel="nofollow">http://stackoverflow.com/questions/31212546/systemclear-equivalent-in-go</a></p></pre>

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

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