Skip to content

Instantly share code, notes, and snippets.

@bisgardo
Created September 5, 2021 11:51
Show Gist options
  • Save bisgardo/3eb29d4f5044d6ff84473368a44674c3 to your computer and use it in GitHub Desktop.
Save bisgardo/3eb29d4f5044d6ff84473368a44674c3 to your computer and use it in GitHub Desktop.
Small Go program to do a binary diff between two files
package main
import (
"fmt"
"io/ioutil"
"os"
)
func unwrapPathError(err error) error {
if e, ok := err.(*os.PathError); ok {
return e.Err
}
return err
}
func main() {
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "usage: %v FILE FILE\n", os.Args[0])
os.Exit(1)
}
f1, f2 := os.Args[1], os.Args[2]
d1, err := ioutil.ReadFile(f1)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot read file '%v': %v\n", f1, unwrapPathError(err))
os.Exit(1)
}
d2, err := ioutil.ReadFile(f2)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot read file '%v': %v\n", f2, unwrapPathError(err))
os.Exit(1)
}
minLen := len(d1)
if l := len(d2); l < minLen {
minLen = l
}
for i := 0; i < minLen; i++ {
b1, b2 := d1[i], d2[i]
if b1 != b2 {
fmt.Printf("%d: %x != %x\n", i, b1, b2)
}
}
fmt.Printf("extra bytes in %s: %d\n", f1, len(d1)-minLen)
fmt.Printf("extra bytes in %s: %d\n", f2, len(d2)-minLen)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment