package module
Version: v0.0.0-...-4e55e63 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2021 License: MIT Imports: 3 Imported by: 3



A small Go library to switch between CamelCase, lowerCamelCase, snake_case, and kebab-case, among others. Features:

  • Unicode support 👩‍💻
  • Intelligent Split function
  • Camel, lowerCamel, snake, and kebab casing built-in
  • Extensible via simple composition
  • Optional handling of initialisms like ID and HTTP


I wrote this library because the below ones don't support unicode, or have strange edge cases, or used a different implementation approach that was less flexible / extensible. They are good projects worth linking to nonetheless, and inspired me to create this library.

Due to the different approaches I decided to create a new library rather than submitting a PR to completely rewrite their projects, potentially introducing unexpected behavior or breaking their existing users.


The library is easy to get started with:

import ""

input := "AnyKind of_string"

// Convert to different cases!

// Unicode works too!

If you would rather convert unicode characters to ASCII approximations, check out rainycape/unidecode as a potential preprocessor.


Each casing function can also take in TransformFunc parameters that apply a transformation to each part of a split string. Here is a simple identity transform showing how to write such a function:

// Identity returns the same part that was passed in.
func Identity(part string) string {
	return part

You may notice the transform function signature matches some existing standard library signatures, for example strings.ToLower and strings.ToUpper. These can be used directly as transform functions without needing to wrap them.

This supports any number of transformations before the casing join operation. For one example use-case, imagine generating Go code and you want to generate a public variable name that might contain an initialism that should be capitalized:

// Generates `UserID` which passes go-lint
name := casing.Camel("USER_ID", strings.ToLower, casing.Initialism)

Transformations are applied in-order and provide a powerful way to customize output for many different use-cases.

If no transformation function is passed in, then strings.ToLower is used by default. You can pass in the identity function to disable this behavior.

Split, Join & Merge

The library also exposes intelligent split and join functions along with the ability to merge some parts together before casing, which are used by the high-level casing functions above.

// Returns ["Any", "Kind", "of", "STRING"]
parts := casing.Split("AnyKind of_STRING")

// Returns "Any.Kind.Of.String"
casing.Join(parts, ".", strings.ToLower, strings.Title)

There is also a function to merge numbers in some cases for nicer output. This is useful when joining with a separator and you want to prevent separators between some parts that are prefixed or suffixed with numbers. for example:

// Splits into ["mp", "3", "player"]
parts := casing.Split("mp3 player")

// Merges into ["mp3", "player"]
merged := casing.MergeNumbers(parts)

// Returns "MP3.Player"
casing.Join(merged, ".", strings.Title, casing.Initialism)

Together these primitives make it easy to build your own custom casing if needed.



Package casing helps convert between CamelCase, snake_case, and others. It includes an intelligent `Split` function to take almost any input and then convert it to any type of casing.



This section is empty.


This section is empty.


func Camel

func Camel(value string, transform ...TransformFunc) string

Camel returns a CamelCase version of the input. If no transformation functions are passed in, then strings.ToLower is used. This can be disabled by passing in the identity function.

func Identity

func Identity(part string) string

Identity is the identity transformation function.

func Initialism

func Initialism(part string) string

Initialism converts common initialisms like ID and HTTP to uppercase.

func Join

func Join(parts []string, sep string, transform ...TransformFunc) string

Join will combine split parts back together with the given separator and optional transform functions.

func Kebab

func Kebab(value string, transform ...TransformFunc) string

Kebab returns a kabob-case version of the input.

func LowerCamel

func LowerCamel(value string, transform ...TransformFunc) string

LowerCamel returns a lowerCamelCase version of the input.

func MergeNumbers

func MergeNumbers(parts []string, suffixes ...string) []string

MergeNumbers will merge some number parts with their adjacent letter parts to support a smarter delimited join. For example, `h264` instead of `h_264` or `mp3-player` instead of `mp-3-player`. You can pass suffixes for right aligning certain items, e.g. `K` to get `MODE_4K` instead of `MODE4_K`. If no suffixes are passed, then a default set of common ones is used. Pass an empty string to disable the default.

func Snake

func Snake(value string, transform ...TransformFunc) string

Snake returns a snake_case version of the input.

func Split

func Split(value string) []string

Split a value intelligently, taking into account different casing styles, numbers, symbols, etc.

// Returns: ["HTTP", "Server", "2020"]


type TransformFunc

type TransformFunc func(string) string

TransformFunc is used to transform parts of a split string during joining.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL