SoFunction
Updated on 2025-03-03

Use Go to find the same record in two large files

To find the same records in two large files in Go, you can use the following strategy:

Ideas

  • Read the file: Read records in two files line by line, assuming that each line of each file represents a record.
  • Use hash sets (Set): Because the hash set can quickly determine whether a record exists, we can put the record in the first file into the set, and then determine whether the record also exists in the set line by line when reading the second file. If present, the same record.
  • Performance optimization
    • If the file is very large, avoid loading it all into memory at once, but process it line by line.
    • If the file is very large and there is duplicate data, you can first deduplicate the data in the file.

Code implementation

package main

import (
    "bufio"
    "fmt"
    "os"
    "log"
)

// Read data from the file and return a map to record the number of occurrences of each linefunc readFileToSet(filename string) (map[string]bool, error) {
    file, err := (filename)
    if err != nil {
        return nil, err
    }
    defer ()

    recordSet := make(map[string]bool)
    scanner := (file)
    for () {
        line := ()
        recordSet[line] = true
    }

    if err := (); err != nil {
        return nil, err
    }

    return recordSet, nil
}

// Find the same record in two filesfunc findCommonRecords(file1, file2 string) ([]string, error) {
    // Read the first file to Set    recordSet, err := readFileToSet(file1)
    if err != nil {
        return nil, err
    }

    // Open the second file and read it line by line    file, err := (file2)
    if err != nil {
        return nil, err
    }
    defer ()

    var commonRecords []string
    scanner := (file)
    for () {
        line := ()
        if recordSet[line] {
            commonRecords = append(commonRecords, line)
        }
    }

    if err := (); err != nil {
        return nil, err
    }

    return commonRecords, nil
}

func main() {
    file1 := ""
    file2 := ""

    commonRecords, err := findCommonRecords(file1, file2)
    if err != nil {
        ("Error finding common records: %v", err)
    }

    ("Common Records:")
    for _, record := range commonRecords {
        (record)
    }
}

Code Analysis

readFileToSet

Used to read records in a file (line by line) to amap[string]boolIn the hash set, make sure that each row of records in the file exists uniquely in the set.

findCommonRecords

Call firstreadFileToSetRead the first file and store it in a hash collectionrecordSetmiddle.

Then open the second file, read line by line and determine whether the record exists in the collection of the first file. If it exists, add the record tocommonRecordsSlice.

main

Set the path of two files and callfindCommonRecordsFunction to find the same record and output the result.

Performance optimization

Reduce memory usage

  • Just load all records of the first file into memory, and the second file is read line by line and judged.
  • If the file is too large, you can use external sorting or process the file in chunks.

Concurrent processing

It is possible to consider concurrent processing of the read operations of two files, or to process different parts of the file in parallel with multiple processors.

Use Cases

AssumptionsandThe contents are as follows:

apple
banana
cherry
grape
orange

pear
banana
grape
watermelon
apple

After running the program, the output result is:

Common Records:
apple
banana
grape

in conclusion

This solution uses hash collections to quickly find, which can efficiently process record comparisons of two large files, and throughRead the file line by line to avoid the problem of loading the entire file into memory at one time.

The above is the detailed content of using Go language to find the same records in two large files. For more information about Go finding the same records in files, please pay attention to my other related articles!