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 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python