登录
首页 >  Golang >  Go问答

如何在读取标准输出时保留脚本的格式?

来源:stackoverflow

时间:2024-02-27 10:09:28 326浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《如何在读取标准输出时保留脚本的格式?》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

我正在 windows 10 上通过 cygwin 执行 shell 脚本,将 stdout 和 stderr 读取为字符串,并将该字符串传递给用于执行 html 模板的结构。

问题是执行模板后不会保留输出的间距。以下是我执行命令并将命令输出设置到我的数据结构的方式。

var outbuf, errbuf bytes.buffer
cmd.stdout = &outbuf
cmd.stderr = &errbuf

err = cmd.run()
if err != nil {
    log.printf("error running command: %s", err)
    data.output = errbuf.string()
    return data
}
log.printf("%v", outbuf.string())
data.output = outbuf.string()
return data

log.printf 会产生类似这样的结果,这正是我所期望的。显示新行。

if the same archived message file is in both system/logs and archived_logs, it will be searched/counted twice!

====x1-sc1====



done

运行命令并更新我的数据结构后,将使用 html 模板执行数据。

err = t.execute(w, data)
if err != nil {
    log.println(err)
    w.writeheader(http.statusinternalservererror)
    w.write([]byte(fmt.sprintf("failed to execute html: %v", err)))
    return
    }
}
{{.output}}

浏览到我的项目公开的位置后,我看到了我期望的输出,但它都在一行上。间距未保留。 html/模板执行似乎正在删除额外的空白、行或类似的东西。有办法解决这个问题吗?

If the same archived message file is in both system/logs and archived_logs, it will be searched/counted twice! Maybe it'll be fixed in the future.. ====X1-SC1==== Done

解决方案


查看源码,会发现模板保留了空格。问题在于浏览器正在折叠空白序列。参见developer.mozilla.org/en-US/docs/Web/CSS/white-space

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《如何在读取标准输出时保留脚本的格式?》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>