duanboshe0001 2018-07-19 07:57
浏览 132
已采纳

如何打印到控制台窗口?

I want to run my go program from cmd like this:

 C:\go\awesomeProject> goprogramm.exe -generate -val=2

and want to get some feedback like this:

> Process started
> Something went wrong. Type e-mail to receive report:
> |...

Yeah, I googled a lot. Standard output, go exec'ing, pipes and etc. Just can't mix it up.

  • 写回答

1条回答 默认 最新

  • drugs3550 2018-07-19 08:03
    关注

    Simplest is to use the fmt package, and its global functions, e.g.:

    fmt.Println("Process started")
    fmt.Println("Something went wrong. Type e-mail to receive report:")
    
    var i int = 3
    var err error = io.Eof
    fmt.Printf("And some formatted text: number: %d, an error: %v
    ", i, err)
    

    If you want more configuration or the option to redirect the output e.g. to a file (in the future), then you may use the log package.

    log.Println("Process started")
    log.Println("Something went wrong. Type e-mail to receive report:")
    
    var i int = 3
    var err error = io.Eof
    log.Printf("And some formatted text: number: %d, an error: %v", i, err)
    

    One notable difference is that by default the fmt package writes to os.Stdout, and the log package writes to os.Stderr. Both appear in your console, but should not be forgotten if you want to redirect those streams. Another difference is that the log package appends a newline to each call if it doesn't end with one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化