dqe9657 2015-09-06 08:40
浏览 747
已采纳

golang-比较两个文本文件

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

  • 写回答

1条回答 默认 最新

  • douliao8318 2015-09-06 10:12
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题