Created
February 3, 2016 20:19
-
-
Save AHaymond/707670dfa8474398d3ad to your computer and use it in GitHub Desktop.
snake case a camel cased word in Go
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
package utils | |
import ( | |
"strings" | |
"unicode" | |
) | |
func SnakeCase(str string) string { | |
var words []string | |
l := 0 | |
for s := str; s != ""; s = s[l:] { | |
l = strings.IndexFunc(s[1:], unicode.IsUpper) + 1 | |
if l <= 0 { | |
l = len(s) | |
} | |
words = append(words, s[:l]) | |
} | |
snaked := strings.Join(words, "_") | |
snaked = strings.ToLower(snaked) | |
return snaked | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment