dongqiao6730 2017-09-21 04:41
浏览 125

如何获取Go lang中两个目录的内容之间的差异

I could get the diff between content of two directories by running the diff command through the os package as follows,

command := "diff --brief -r /home/kasun/Documents/Old-directory/ " +
    "/home/kasun/Documents/New-directory/"

out, err := exec.Command("sh", "-c", command).Output()
if err != nil {
    fmt.Println("error occured")
    fmt.Printf("%s", err)
}
fmt.Printf("%s", out)

since diff is a command only available in bash, the binary formed by building the above code cannot be used to get diff in a Windows system. Is there any other way to achieve the same so that it will work on any platform, like performing the diff using standard go libraries. Thanks in advance

  • 写回答

2条回答 默认 最新

  • duanqiao1949 2017-09-21 04:58
    关注

    since diff is a command only available in bash, the binary formed by building the above code cannot be used to get diff in a Windows system

    It can, if you install (unzip anywhere you want) the latest Portable Git (like PortableGit-2.14.1-64-bit.7z.exe)

    If you add that installation to your PATH, you do have diff (to be used in your Go program)

    vonc@VONCAVN7 C:\
    > where diff
    D:\prgs\git\latest\usr\bin\diff.exe
    

    But if you don't want to rely on an existing thrid-party installed software (here Git), you might consider vendoring in your project spcau/godiff.
    If does have a diff implementation for Windows.


    Note that, with Git 2.17 (Q2 2018), Go is better recognized by Git, since "git diff" and friends learned funcname patterns for Go language source files.

    See commit 1dbf0c0 (01 Mar 2018) by Alban Gruin (``).
    (Merged by Junio C Hamano -- gitster -- in commit 077cde9, 08 Mar 2018)

    userdiff: add built-in pattern for Golang

    This adds xfuncname and word_regex patterns for Golang, a quite popular programming language. It also includes test cases for the xfuncname regex (t4018) and updated documentation.

    The xfuncname regex finds functions, structs and interfaces.
    Although the Go language prohibits the opening brace from being on its own line, the regex does not makes it mandatory, to be able to match func statements like this:

    func foo(bar int,
        baz int) {
    }
    

    This is covered by the test case t4018/golang-long-func.

    The word_regex pattern finds identifiers, integers, floats, complex numbers and operators, according to the go specification.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度