split包括EOF吗?

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

split includes EOF?

问题

我正在尝试在Windows 7上使用Go版本1.7.3运行Donovan书中的gopl.io/ch1/dup3程序。

当我运行下面的test.go程序时,最后会得到一个空行。这是表示文件结束的标志吗?我该如何将其与实际的空行区分开来?

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "strings"
  7. )
  8. func main() {
  9. counts := make(map[string]int)
  10. for _, filename := range os.Args[1:] {
  11. data, err := ioutil.ReadFile(filename)
  12. if err != nil {
  13. fmt.Fprintf(os.Stderr, "%s: %v\n", os.Args[0], err)
  14. continue
  15. }
  16. for _, line := range strings.Split(string(data), "\r\n") {
  17. counts[line]++
  18. }
  19. }
  20. for line, n := range counts {
  21. if n > 1 {
  22. fmt.Printf("%d\t%s\n", n, line)
  23. }
  24. }
  25. }

使用test.dat文件运行命令:

  1. > test.exe test.dat test.dat

输出结果为:

  1. 2 Line number one
  2. 2 Line number two
  3. 2 <-- 这是空行。

请注意,我只会返回翻译好的部分,不会回答关于翻译的问题。

英文:

I'm trying to run gopl.io/ch1/dup3 program from Donovan book on windows 7 using go version 1.7.3.

When I run the below program test.go, I get an empty line at the end. Is that for EOF? How do I distinguish it from an actual empty line?

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;io/ioutil&quot;
  5. &quot;os&quot;
  6. &quot;strings&quot;
  7. )
  8. func main() {
  9. Counts := make(map[string]int)
  10. for _, filename := range os.Args[1:] {
  11. data, err := ioutil.ReadFile(filename)
  12. if err != nil {
  13. fmt.Fprintf(os.Stderr, &quot;%s: %v\n&quot;, os.Args[0], err)
  14. continue
  15. }
  16. for _, line := range strings.Split(string(data), &quot;\r\n&quot;) {
  17. counts
    ++
  18. }
  19. }
  20. for line, n := range counts {
  21. if n &gt; 1 {
  22. fmt.Printf(&quot;%d\t%s\n&quot;, n, line)
  23. }
  24. }
  25. }

with test.dat file:

  1. Line number one
  2. Line number two

Command:

  1. &gt; test.exe test.dat test.dat

Output is

  1. 2 Line number one
  2. 2 Line number two
  3. 2 &lt;-- Here is the empty line.

答案1

得分: 1

如果你的文件以换行序列结尾,将文件内容按换行序列进行拆分将导致多出一个空字符串。如果在读取最后一个换行序列之前发生了EOF,则不会得到该空字符串:

  1. eofSlice := strings.Split("Hello\r\nWorld", "\r\n")
  2. extraSlice := strings.Split("Hello\r\nWorld\r\n", "\r\n")
  3. // [Hello World] 2
  4. fmt.Println(eofSlice, len(eofSlice))
  5. // [Hello World ] 3
  6. fmt.Println(extraSlice, len(extraSlice))

Playground链接

英文:

If your file ends with a newline sequence, splitting the file content on newline sequences will result in an extraneous empty string. If EOF occurred before a final newline sequence was read, then you don't get that empty string:

  1. eofSlice := strings.Split(&quot;Hello\r\nWorld&quot;, &quot;\r\n&quot;)
  2. extraSlice := strings.Split(&quot;Hello\r\nWorld\r\n&quot;, &quot;\r\n&quot;)
  3. // [Hello World] 2
  4. fmt.Println(eofSlice, len(eofSlice))
  5. // [Hello World ] 3
  6. fmt.Println(extraSlice, len(extraSlice))

Playground link

huangapple
  • 本文由 发表于 2016年11月27日 23:59:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/40830868.html
匿名

发表评论

匿名网友

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

确定