I want to know the contents when file changed
there is a simeple txt file
textOne 1,2,3,4,5,6,7,8,9,10
and i change it to
textTwo 1,2,3,4,7,7,7,8,9,10
so 'One' and '5,6' changed to 'two', '7,7'
I can find things which changed by loop. but I wonder if there was some better way to check
Instead of looping use standard third-party library, with small optimisation.
One of the library that I've used in past is Go port of google-diff-match-patch (same library that @Not_a_golfer suggested in the comment.).
You can optimise this by first calculating sha2
hash of two files, and if they are not the same, you can assume that they are changed, otherwise (probably) they are same, and skip the diff operation.
One drawback of this optimisation is that, because of pigeon-hole principle, it is possible theoretically to have same hash value, for different contents. But, the probability of happening that is quite small.
EDIT (based on @elithrar's comment): Since calculation of hash for very large file, can be time consuming. You can calculate sha2
in chunks (size depends on particular hash algorithm from sha2
family). This enables you to bail out early, and improves speed.