amuz.es/src/go/misc@v1.0.1/strutil/convention.go (about)

     1  package strutil
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  )
     7  
     8  var (
     9  	camelCase = regexp.MustCompile("(^[^A-Z0-9]*|[A-Z0-9]*)([A-Z0-9][^A-Z]+|$)")
    10  )
    11  
    12  // CamelToUnderscore method converts a naming convention from CamelCase to under_score.
    13  func CamelToUnderscore(s string) string {
    14  	var a []string
    15  	for _, sub := range camelCase.FindAllStringSubmatch(s, -1) {
    16  		if sub[1] != "" {
    17  			a = append(a, sub[1])
    18  		}
    19  		if sub[2] != "" {
    20  			a = append(a, sub[2])
    21  		}
    22  	}
    23  	return strings.ToLower(strings.Join(a, "_"))
    24  }