我试图比较两个单独的文件充满了链接(约422行),并放置任何新的行/链接到第三个文件使用Golang。任何想法如何完成这一点?
我已经尝试了下面的代码来比较文档,但不断得到奇怪的结果(计数300新行时,它应该只有10),并没有存储任何新的链接,因为我不知道如何做到这一点。
// 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)
}
}
1条答案
按热度按时间2vuwiymt1#
@icza是对的,使用Map
如果
http://example.com/?a=1&b=2
与http://example.com/?b=2&a=1
不相同:下面是一个例子: