Created
January 8, 2014 11:01
-
-
Save majest/8315109 to your computer and use it in GitHub Desktop.
String comparing method in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func StringCompare(a, b string) int { | |
var min = len(b) | |
if len(a) < len(b) { | |
min = len(a) | |
} | |
var diff int | |
for i := 0; i < min && diff == 0; i++ { | |
diff = int(a[i]) - int(b[i]) | |
} | |
if diff == 0 { | |
diff = len(a) - len(b) | |
} | |
return diff | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment