strcase is a go package for converting string case to various cases (e.g. snake case or camel case) to see the full conversion table below.
s := "AnyKind of_string"
Function | Result |
---|---|
ToSnake(s) |
any_kind_of_string |
ToSnakeWithIgnore(s, '.') |
any_kind.of_string |
ToScreamingSnake(s) |
ANY_KIND_OF_STRING |
ToKebab(s) |
any-kind-of-string |
ToScreamingKebab(s) |
ANY-KIND-OF-STRING |
ToDelimited(s, '.') |
any.kind.of.string |
ToScreamingDelimited(s, '.', '', true) |
ANY.KIND.OF.STRING |
ToScreamingDelimited(s, '.', ' ', true) |
ANY.KIND OF.STRING |
ToCamel(s) |
AnyKindOfString |
ToLowerCamel(s) |
anyKindOfString |
go get -u github.com/iancoleman/strcase
Often times text can contain specific acronyms which you need to be handled a certain way.
Out of the box strcase
treats the string "ID" as "Id" or "id" but there is no way to cater
for every case in the wild.
To configure your custom acronym globally you can use the following before running any conversion
import (
"github.com/iancoleman/strcase"
)
func init() {
// results in "Api" using ToCamel("API")
// results in "api" using ToLowerCamel("API")
strcase.ConfigureAcronym("API", "api")
// results in "PostgreSQL" using ToCamel("PostgreSQL")
// results in "postgreSQL" using ToLowerCamel("PostgreSQL")
strcase.ConfigureAcronym("PostgreSQL", "PostgreSQL")
}