<p>Hey!Could anyone show me the equivalent in go to the system("clear"); 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 "os"
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("\x1b[3;J\x1b[H\x1b[2J")</code></p></pre>TeranNotTerran: <pre><p>Another:</p>
<pre><code>package main
import (
"os"
"os/exec"
)
func main() {
out, _ := exec.Command("/usr/local/bin/clear").Output()
os.Stdout.Write(out)
}
</code></pre></pre>barsonme: <pre><p>Is this what you'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't work on windows fyi</p></pre>seomis: <pre><p>All you need in windows is</p>
<p>package main</p>
<p>import (
"os"
"os/exec"
)</p>
<p>func main() {
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
}</p>
<p>almost the same!</p></pre>esesci: <pre><p>you won'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 == "windows" {
fmt.Println("Hello from Windows")
}</p></pre>esesci: <pre><p>it'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 "draw" to the console.
<a href="https://github.com/rthornton128/goncurses" rel="nofollow">https://github.com/rthornton128/goncurses</a><br/>
There'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'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'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're clearing the console, you'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传