在一个空的 bash 窗口中打开文件的 Golang 代码。

huangapple go评论75阅读模式
英文:

golang - open file in an empty bash window

问题

让我们假设我有以下代码:

package main

import (
    "io/ioutil"
    "fmt"
)

func check(err error) {
    if err != nil {
        panic(err)
    }
}

func main() {
    file, err := ioutil.ReadFile("test.txt")
    check(err)
    fmt.Print(string(file))
}

当使用go run运行它时,我希望它在一个清晰的bash窗口中显示。是否可以在不使用任何额外的开源库的情况下实现这一点?

提前感谢。

英文:

lets say I have this code:

package main 

import (
     "io/ioutil" 
     "fmt"
)
func check(err error) {
     if err != nil {
         panic(err)
     }
} 
func main() {
     file, err := ioutil.ReadFile("test.txt")
     check(err)
     fmt.Print(string(file))
}

when running it with go run I want it to be written in a cleared bash window. Is it possible to do so without using any additional open-source repositories?

Thanks in advance.

答案1

得分: 1

如果清除终端确实是你的程序责任的一部分,那么可以参考这个问题的答案:https://stackoverflow.com/questions/22891644/how-can-i-clear-the-terminal-screen-in-go

然而,如果你只是想在开发过程中清除屏幕,那么可以简单地执行以下操作:

clear && go run *.go
英文:

If clearing the terminal is truly part of your program's responsibility then check out the answers in this question https://stackoverflow.com/questions/22891644/how-can-i-clear-the-terminal-screen-in-go

However if you're just wanting to clear the screen as part of your development process then I would keep it simple and do something like this

clear && go run *.go

huangapple
  • 本文由 发表于 2016年11月7日 23:07:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/40468404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定