I would like to edit one line of text from a large plain text file, and want to do this inplace, like Python's fileinput package:
fileinput.input(file, inplace=1)
My current approach is to read the whole file into []string and write them back, and I think this is terribly inefficient. So what is idiomatic way to do this in Golang?
Thanks.
Note that Python's inplace Fileinput works by copying the file to backup file and then redirecting the output to the original file. So it's not all that different from your current approach except that it uses a temporary file instead of loading it in memory. Loading it in memory could be ok, unless the file is very large.
If the file is large, I suggest copying it to a temporary directory and then use the functions from the bufio
package to read it line by line, modify what's needed and write the result to a new file with the same name as the original.