22 lines
407 B
Go
22 lines
407 B
Go
package util
|
|
|
|
import (
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
func CamelToSnake(str string) string {
|
|
var snake strings.Builder
|
|
isPrevUpper := true
|
|
for _, c := range str {
|
|
if unicode.IsUpper(c) && !isPrevUpper {
|
|
snake.WriteString("_" + string(unicode.ToUpper(c)))
|
|
isPrevUpper = true
|
|
} else {
|
|
snake.WriteString(string(unicode.ToUpper(c)))
|
|
isPrevUpper = unicode.IsUpper(c)
|
|
}
|
|
}
|
|
return snake.String()
|
|
}
|