We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
A simple suggestion for emulating the open() function of Python. as in
f = open("demofile.txt", "r") for x in f: print(x)
Aim: Reading a file line by line in order to avoid reading a very large file all at once (to save RAM).
reading a file, line by line in a for loop can be done in Go the following way
func Open(fname string) (error, *os.File) { file, err := os.Open(fname) check(err) return err, file } func Close(file *os.File) { file.Close() } func ReadLines(f *os.File) chan string { ch_lines := make(chan string) go func() { defer close(ch_lines) scanner := bufio.NewScanner(f) for scanner.Scan() { ch_lines <- scanner.Text() } }() return ch_lines }
Examples of use:
_, f := fi.Open("text.txt") lines := fi.ReadLines(f) for line := range lines { println(line) }
more details here: https://github.com/serge-hulne/goutils/blob/main/files/fileutils.go
The text was updated successfully, but these errors were encountered:
f68ea55
Merge pull request #1324 from xushiwei/top
5db3eaf
close #1320
import "os" for line <- os.Stdin { println line }
and Go+ provides builtin functions open and create for os.File.
open
create
Sorry, something went wrong.
% cat foo.gop
for line <- open("foo.gop")! { println line }
% gop run foo.gop
No branches or pull requests
Proposal
A simple suggestion for emulating the open() function of Python.
as in
Background
Aim: Reading a file line by line in order to avoid reading a very large file all at once (to save RAM).
Workarounds
reading a file, line by line in a for loop can be done in Go the following way
Examples of use:
more details here:
https://github.com/serge-hulne/goutils/blob/main/files/fileutils.go
The text was updated successfully, but these errors were encountered: