簡體   English   中英

比較 go 中的兩個文件並將任何新行復制到新文件

[英]Comparing two files in go and any new lines get copied to a new file

我正在嘗試比較兩個充滿鏈接(大約 422 行)的單獨文檔,並使用 Golang 將任何新行/鏈接放入第三個文檔。 關於如何完成這個的任何想法?

我已經嘗試使用下面的代碼來比較文檔,但結果一直很奇怪(當它應該只有 10 行時,卻計算了 300 行新行)並且我沒有存儲任何新鏈接,因為我不確定如何這樣做。


// code to compare documents
func Compare() {
    // open the first document
    file1, err := os.Open("Links_New.txt")
    if err != nil {
        fmt.Println("Error opening file:", err)
        return
    }
    defer file1.Close()

    // open the second document
    file2, err := os.Open("Links_Old.txt")
    if err != nil {
        fmt.Println("Error opening file:", err)
        return
    }
    defer file2.Close()

    // create scanners for both documents
    scanner1 := bufio.NewScanner(file1)
    scanner2 := bufio.NewScanner(file2)

    // initialize a counter for the number of New lines
    NewLines := 0

    // loop through each line in the first file
    for scanner1.Scan() {
        // check if the line exists in the second file
        if scanner2.Scan() {
            if scanner1.Text() != scanner2.Text() {
                NewLines++
            }
        } else {
            break
        }
    }

    // check for errors while scanning
    if err := scanner1.Err(); err != nil {
        fmt.Println("Error scanning file:", err)
        return
    }
    if err := scanner2.Err(); err != nil {
        fmt.Println("Error scanning file:", err)
        return
    }

    // print the number of new lines
    fmt.Println("\n\n Number of new lines:", NewLines)
}

// code to transfer links new to old
func ReadWrite() {
    // Open the source file for reading
    src, err := os.Open("Links_New.txt")
    if err != nil {
        panic(err)
    }
    defer src.Close()

    // Open the destination file for writing
    dst, err := os.Create("Links_Old.txt")
    if err != nil {
        panic(err)
    }
    defer dst.Close()

    // Copy the contents of the source file to the destination file
    _, err = io.Copy(dst, src)
    if err != nil {
        panic(err)
    }
}

@icza 是對的,使用 map

如果http://example.com/?a=1&b=2http://example.com/?b=2&a=1

下面是一個例子:

package main

import (
    "bufio"
    "fmt"
    "io"
    "os"
)

var m = map[string]bool{}

func Compare() {
    // open the first document
    file1, _ := os.Open("Links_New.txt")
    defer file1.Close()

    // open the second document
    file2, _ := os.Open("Links_Old.txt")
    defer file2.Close()

    // create scanners for both documents
    r1 := bufio.NewReader(file1)

    // initialize a counter for the number of New lines
    NewLines := 0
    // loop through each line in the first file
    for {
        line, _, c := r1.ReadLine()
        m[string(line)] = true
        if c == io.EOF {
            break
        }
    }
    r2 := bufio.NewReader(file2)
    for {
        line, _, c := r2.ReadLine()
        if !m[string(line)] {
            NewLines++
            fmt.Printf("%s\n", line)
        }
        if c == io.EOF {
            break
        }
    }
    // print the number of new lines
    fmt.Println("\n\n Number of new lines:", NewLines)
}

func main() {
    Compare()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM