David's Blog

Building a text diffing tool in golang

By David Li on Fri, 17 January 2024


Text diff tools are essential for comparing two versions of a document or codebase to identify changes made between them. These tools are widely used in software development, content management, and version control systems. In this article, we will explain how to build a text diff tool in Golang, a popular programming language for building efficient and high-performance applications.


Before starting, ensure that you have the following:

  • A basic understanding of Golang
  • A text editor or IDE installed on your system
  • Golang installed on your system

Understanding the Algorithm

The algorithm used to compare two text files or documents is known as the Longest Common Subsequence (LCS). The LCS algorithm compares two strings and returns the longest sequence of characters that are common between the two strings. This algorithm is used in most text diff tools to identify the differences between two text files.

LCS algorithm works as follows:

  1. Create a matrix of the two strings where the columns and rows represent the characters of the respective strings.

  2. Initialize the first row and column with zero.

  3. Compare each character of the two strings. If the characters match, set the value of the current cell to the diagonal cell value plus one.

  4. If the characters do not match, set the value of the current cell to the maximum of the left and top cell values.

  5. Traverse the matrix to find the longest sequence of characters.

Building the Text Diff Tool

Now that we understand the algorithm, let’s build a text diff tool in Golang. This tool will compare two text files and display the differences between them.

Step 1: Import the Required Packages

To implement the LCS algorithm, we need to import the “fmt” and “os” packages. The “os” package is used to read the file contents.

package main

import (

Step 2: Define the LCS Function

The LCS function takes two strings as input and returns the longest common subsequence. We will implement the LCS function using the algorithm explained earlier.

func LCS(s1 string, s2 string) string {
    m := len(s1)
    n := len(s2)
    lcs := make([][]int, m+1)
    for i := range lcs {
        lcs[i] = make([]int, n+1)
    for i := 0; i <= m; i++ {
        for j := 0; j <= n; j++ {
            if i == 0 || j == 0 {
                lcs[i][j] = 0
            } else if s1[i-1] == s2[j-1] {
                lcs[i][j] = lcs[i-1][j-1] + 1
            } else {
                lcs[i][j] = max(lcs[i-1][j], lcs[i][j-1])
    index := lcs[m][n]
    lcsStr := make([]byte, index)
    i := m
    j := n
    for i > 0 && j > 0 {
        if s1[i-1] == s2[j-1] {
            lcsStr[index-1] = s1[i-1]
        } else if lcs[i-1][j] > lcs[i][j-1] {
        } else {
    return string(lcsStr)

func max(a, b int) int {
    if a > b {
        return a
    return b

Step 3: Define the Main Function

The main function reads the contents of the two files, passes them to the LCS function, and displays the differences between them.

func main() {
    if len(os.Args) != 3 {
        fmt.Println("Usage: go run textdiff.go file1 file2")
    file1 := os.Args[1]
    file2 := os.Args[2]
    content1, err := os.ReadFile(file1)
    if err != nil {
        fmt.Printf("Error reading %s: %v\n", file1, err)
    content2, err := os.ReadFile(file2)
    if err != nil {
        fmt.Printf("Error reading %s: %v\n", file2, err)
    str1 := string(content1)
    str2 := string(content2)
    lcs := LCS(str1, str2)
    fmt.Printf("File1:\n%s\n\nFile2:\n%s\n\nDifferences:\n%s", str1, str2, lcs)

Step 4: Build and Run the Program

Save the above code in a file named “textdiff.go”. Use the following command to build and run the program:

go run textdiff.go file1.txt file2.txt Replace “file1.txt” and “file2.txt” with the actual file names you want to compare. The program will display the contents of both files and the differences between them.


In this article, we have explained how to build a text diff tool in Golang using the Longest Common Subsequence algorithm. The program compares two text files and displays the differences between them. You can modify the program to suit your needs or use it as a basis for building more complex text diff tools. Golang’s efficiency and performance make it an ideal language for building such tools.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-02-29.