使用Golang的log包将行追加到文件中时,没有换行。

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

Appending line to file with Golang Package log, not getting a new line.

问题

我正在使用golang的"log"包,当我使用log写入文本文件时,它不会在末尾添加新的文本行,而是将所有内容放在一行中。

这是它的显示效果。

这是我希望它的显示效果。

当前的代码。

f, err := os.OpenFile("D:\\temp2\\testlogfile.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
	Error.Fatalf("打开文件时出错:%v", err)
}
defer f.Close()
multi := io.MultiWriter(os.Stdout, f)
log.SetOutput(multi)
log.Println("testy1")
log.Println("testy2")

有人有什么想法吗?

英文:

I am using the golang package "log" when I use log to write to a text file, it does not append a new line of text to the end. It puts everything inline.

This is how it looks.

How I would like it to look.

Current Code.

f, err := os.OpenFile("D:\\temp2\\testlogfile.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
	Error.Fatalf("error opening file: %v", err)
}
defer f.Close()
multi := io.MultiWriter(os.Stdout, f)
log.SetOutput(multi)
log.Println("testy1")
log.Println("testy2")

Does anyone have any ideas?

答案1

得分: 2

如评论中已经提到的,你的编辑器不理解Unix风格的“LF”\n换行符。建议使用Notepad++。

https://notepad-plus-plus.org/

英文:

As already mentioned inside the comments, your editor doesn't understand unix-style "LF" \n newlines. <br />
Consider using Notepad++

https://notepad-plus-plus.org/

答案2

得分: -1

我在这里为您提供翻译服务,以下是您要翻译的内容:

我在内部使用的所有编辑器中,使用\r来表示回车符。它对所有编辑器都有效。

英文:

I used \r for a carriage return. It works fine for all of the editors we use internally.

huangapple
  • 本文由 发表于 2016年1月12日 08:16:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/34733503.html
匿名

发表评论

匿名网友

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

确定