You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
17 lines
366 B
Go
17 lines
366 B
Go
2 years ago
|
package converter
|
||
|
|
||
|
import (
|
||
|
"regexp"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func CamelToSnakeCase(str string) string {
|
||
|
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
|
||
|
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
|
||
|
|
||
|
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
|
||
|
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
|
||
|
|
||
|
return strings.ToLower(snake)
|
||
|
}
|