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

golang文件按行读取 #20

Open
Vexth opened this issue Apr 4, 2018 · 0 comments
Open

golang文件按行读取 #20

Vexth opened this issue Apr 4, 2018 · 0 comments

Comments

@Vexth
Copy link
Owner

Vexth commented Apr 4, 2018

Go语言中文件按行读取的方法:

func readsLine(path string, fn func(string)) {
	f, err := os.Open(path)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	buf := bufio.NewReader(f)
	for {
		line, err := buf.ReadString('\n')
		if err != nil && io.EOF == err {
			break
		}

		fn(line)
	}
}

其中path为要打开文件的路径,fn为操作读取到数据的方法,func(string)为操作方法的类型

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