I'm trying to compare two seperate documents filled with links (approx 422 lines) and place any new lines/links into a third document using Golang. Any idea on how to acomplish this?
I have tried the below code to compare the documents but keep getting odd results (counting 300 new lines when it should only be 10) and am not storing any of the new links as I am unsure how to do so.
// 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 is right, using map
if http://example.com/?a=1&b=2
not same as http://example.com/?b=2&a=1
:
follow is a example:
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()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.