Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core -> filex-> file.go -> firstLine has error #4217

Open
Owen-Zhang opened this issue Jun 27, 2024 · 0 comments
Open

core -> filex-> file.go -> firstLine has error #4217

Owen-Zhang opened this issue Jun 27, 2024 · 0 comments

Comments

@Owen-Zhang
Copy link

Owen-Zhang commented Jun 27, 2024

发现两个问题:

  1. 如果内容为空(什么都没有,包括没有\n\r什么的),程序会出现死 loop, 如果 if n== 0 { return string(first), nil }
  2. 没有对前端有空行(\r\n, \n, \t)这些字节进行处理, 如果前面有空行,返回的第一行数据有问题
  3. lastline也有同样的问题

firstline 下面这样怎么样

func FirstLine(filename string) (string, error) {
	file, err := os.Open(filename)
	if err != nil {
		return "", err
	}
	defer file.Close()

	firstLine := ""
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		temp := scanner.Text()
		if len(temp) == 0 {
			continue
		}
		firstLine = temp
		break
	}
	if err := scanner.Err(); err != nil {
		return "", err
	}
        return firstLine, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant