如何在读取标准输出时保留脚本的格式?
时间:2024-02-27
浏览: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
}
}
<div style="text-align:center">
{{.output}}
</div>
浏览到我的项目公开的位置后,我看到了我期望的输出,但它都在一行上。间距未保留。 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学习网公众号了解相关技术文章。
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.30
11.30
11.30
11.30
11.30
11.30
11.30
11.30
热门专题
