Skip to content

Instantly share code, notes, and snippets.

@briandowns
Created October 22, 2014 02:07
Show Gist options
  • Save briandowns/f57b5affcef6417bc833 to your computer and use it in GitHub Desktop.
Save briandowns/f57b5affcef6417bc833 to your computer and use it in GitHub Desktop.
Go Hamming Distance
package main
import (
"errors"
"log"
)
func getDistance(s1, s2 string) (int, error) {
if len(s1) != len(s2) {
return 0, errors.New("ERROR: Strings are of different lengths")
}
var differences int
for i, x := range s1 {
if string(x) != string(s2[i]) {
differences++
}
}
return differences, nil
}
func main() {
distance, err := getDistance("GAGCCTACTAACGGGAT", "ATCGTAATGACGGCCT")
if err != nil {
log.Fatalln(err)
}
log.Printf("Hamming Distance: %d\n", distance)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment