I got a file that other processes are currently writing and appending a new entry at the end of the file. I'm trying to figure out the most efficient way to do this in GO-Lang
I got a program currently written in PERL that does the following:
1)Make a copy of the current file
2)Checks the n of lines in the copied file
3)Truncates the n number of lines from the beginning of the original file
4)Original file retains any file handle that may have been writing to it during the time of the copy.
if ( -s $file){
system("cp -f $file $file_copy");
//Get the number of lines copied
$tmp_linecount =`wc -l < $file_copy`;
chomp($tmp_linecount);
# truncate orignal file from beginning
$command="sed '1," . $tmp_linecount ."d' $file | tee $file";
system($command);
$linecount=`wc -l < $file`;
chomp($linecount);
logh("$file_copy has $tmp_linecount records to process, $file has $linecount records left");
}
Now i'm trying to solve this same problem in GO-Lang. I looked at truncating the file but it looks like it will truncate a file all the way down to the number of btyes you specified.
err := os.Truncate("test.txt", 100)
if err != nil {
log.Fatal(err)
}
At the end of the day What i'm trying to accomplish is reading a file that is currently adding new entries to the EOF. Process any new entries and delete the entries right after without stopping other processes from writing to the file at the same time.