github.com/sandwich-go/boost@v1.3.29/xstrings/snake_case.go (about) 1 package xstrings 2 3 import ( 4 "regexp" 5 "strings" 6 ) 7 8 var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)") 9 var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])") 10 11 // SnakeCase 返回字符串的SnakeCase版本 12 func SnakeCase(str string) string { 13 snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}") 14 snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}") 15 return strings.ToLower(snake) 16 }