Last active
March 29, 2022 16:59
-
-
Save stoewer/fbe273b711e6a06315d19552dd4d33e6 to your computer and use it in GitHub Desktop.
Convert camel case to snake case in Go http://play.golang.org/p/9ybqJat1Hr
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
import ( | |
"fmt" | |
"strings" | |
"regexp" | |
) | |
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)") | |
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])") | |
func ToSnakeCase(str string) string { | |
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}") | |
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}") | |
return strings.ToLower(snake) | |
} |
@abdennour just replace '_' with '-' in the code, and change the function name. I think it will work.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good guys ! keep it up!
kebab case should be concluded here as well