@@ -0,0 +1,21 @@
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user