CRC32 Checksum Calculation via GO

Issue

Trying to create a GO function that produces the same result as the Ubuntu Linux "cksum" operation, for example:

$ echo 123 > /tmp/foo
$ cksum /tmp/foo
2330645186 4 /tmp/foo

Could someone please provide a GO function that produces the first substring of the above result ("2330645186")? Thank you.

Solution

Actually, I found a more simplified answer to my original question:

Using:
https://pkg.go.dev/github.com/cxmcc/unixsums#section-readme

Here is the snippet that provides the posix checksum equivalent value of a file in Go:

path := "/tmp/test.loop"
data, err := ioutil.ReadFile(path)
if err != nil {
    log.Fatal(err)
}
h := cksum.New()
io.WriteString(h, string(data))
fmt.Printf("cksum: %d\n", h.Sum32())

Answered By – Ben Jafari

Answer Checked By – Dawn Plyler (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.