// Package strcase converts strings to snake_case or CamelCase package strcase import ( "strings" ) // Converts a string to snake_case func ToSnake(s string) string { s = addWordBoundariesToNumbers(s) s = strings.Trim(s, " ") n := "" for i, v := range s { // treat acronyms as words, eg for JSONData -> JSON is a whole word nextCaseIsChanged := false if i+1 < len(s) { next := s[i+1] if (v >= 'A' && v <= 'Z' && next >= 'a' && next <= 'z') || (v >= 'a' && v <= 'z' && next >= 'A' && next <= 'Z') { nextCaseIsChanged = true } } if i > 0 && n[len(n)-1] != '_' && nextCaseIsChanged { // add underscore if next letter case type is changed if v >= 'A' && v <= 'Z' { n += "_" + string(v) } else if v >= 'a' && v <= 'z' { n += string(v) + "_" } } else if v == ' ' { // replace spaces with underscores n += "_" } else { n = n + string(v) } } n = strings.ToLower(n) return n }