Skip to content

Instantly share code, notes, and snippets.

@AHaymond
Created February 3, 2016 20:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AHaymond/707670dfa8474398d3ad to your computer and use it in GitHub Desktop.
Save AHaymond/707670dfa8474398d3ad to your computer and use it in GitHub Desktop.
snake case a camel cased word in Go
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