stringFormatter

package module
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: Apache-2.0 Imports: 4 Imported by: 31

README

Wissance/StringFormatter

GitHub go.mod Go version (subdirectory of monorepo) GitHub code size in bytes GitHub issues GitHub Release Date Wissance.StringFormatter CI Go Report Card Coverage Status

StringFormatter is a high-performance Go library for string (text) formatting. It offers a syntax familiar to C#, Java and Python developers (via template arguments - {0} (positional), {name} (named)), extensive argument formatting options (numbers, lists, code style), and a set of text utilities — all while being significantly fast as the standard fmt package and even more! Typical usage of sf are:

  1. Create e-mails, Messages (SMS, Telegram, other Notifications) or other complicated text based on templates
  2. Create a complicated log text with specific argument formatting
  3. Use for template-based src code generator
  4. Other text proccessing

Other important resources:

✨ 1 Features

🔤 Flexible Syntax: Supports both indexed / positional ({0}) and named ({user}) placeholders in templates.

🎨 Advanced Formatting: Built-in directives for numbers (HEX, BIN), floats, lists, and code styles (camelCase, SNAKE_CASE).

🚀 Performance: Template formatting and slice conversion are even faster then fmt.

🛠 Utilities: Functions to convert maps and slices into strings with custom separators and formats.

👨💻 Safe : StringFormatter aka sf is safe (SAST and tests are running automatically on push)

📦 2 Installation

go get github.com/Wissance/stringFormatter

🚀 3 Usage

3.1 Template Formatting
3.1.1 By Argument Order (Format)

The Format function replaces {n} placeholders with the corresponding argument in the provided order.

package main

import (
    "fmt"
    sf "github.com/Wissance/stringFormatter"
)

func main() {
    template := "Hello, {0}! Your balance is {1} USD."
    result := sf.Format(template, "Alex", 2500)
    fmt.Println(result)
    // Output: Hello, Alex! Your balance is 2500 USD.
}
3.1.2 By Argument Name (FormatComplex)

The FormatComplex function uses a map[string]any to replace named placeholders like {key}.

package main

import (
    "fmt"
    sf "github.com/Wissance/stringFormatter"
)

func main() {
    template := "User {user} (ID: {id}) logged into {app}."
    args := map[string]any{
        "user": "john_doe",
        "id":   12345,
        "app":  "dashboard",
    }
    result := sf.FormatComplex(template, args)
    fmt.Println(result)
    // Output: User john_doe (ID: 12345) logged into dashboard.
}
3.2 Advanced Argument Formatting

You can control how arguments are displayed by adding a colon (:) and a format specifier to the placeholder.

Type Specifier Description Example Template Example Value Output
Numbers :B Binary (without padding) "{0:B}" 15 1111
:B8 Binary with 8-digit padding "{0:B8}" 15 00001111
:X Hexadecimal (lowercase) "{0:X}" 250 fa
:X4 Hexadecimal with 4-digit padding "{0:X4}" 250 00fa
:o Octal "{0:o}" 11 14
Floating Point :F Default float format "{0:F}" 10.4567890 10.456789
:F2 Float with 2 decimal places "{0:F2}" 10.4567890 10.46
:F4 Float with 4 decimal places "{0:F4}" 10.4567890 10.4568
:F8 Float with 8 decimal places "{0:F8}" 10.4567890 10.45678900
:E2 Scientific notation "{0:E2}" 191.0478 1.91e+02
Percentage :P100 Percentage (multiply by 100) "{0:P100}" 12 12%
Lists (Slices) :L- Join with hyphen "{0:L-}" [1 2 3] 1-2-3
:L, Join with comma and space "{0:L, }" [1 2 3] 1, 2, 3
Code Styles :c:snake Convert to snake_case "{0:c:snake}" myFunc my_func
:c:Snake Convert to Snake_Case (PascalSnake) "{0:c:Snake}" myFunc My_Func
:c:SNAKE Convert to SNAKE_CASE (upper) "{0:c:SNAKE}" read-timeout READ_TIMEOUT
:c:camel Convert to camelCase "{0:c:camel}" my_variable myVariable
:c:Camel Convert to CamelCase (PascalCase) "{0:c:Camel}" my_variable MyVariable
:c:kebab Convert to kebab-case "{0:c:kebab}" myVariable my-variable
:c:Kebab Convert to Kebab-Case (PascalKebab) "{0:c:Kebab}" myVariable My-Variable
:c:KEBAB Convert to KEBAB-CASE (upper) "{0:c:KEBAB}" myVariable MY-VARIABLE
package main

import (
    "fmt"
    sf "github.com/Wissance/stringFormatter"
)

func main() {
    template := "Status 0x{0:X4} (binary: {0:B8}), temp: {1:F1}°C, items: {2:L, }."
    result := sf.Format(template, 475, 23.876, []int{1, 2, 3})
    fmt.Println(result)
    // Output: Status 0x01DB (binary: 00011101), temp: 23.9°C, items: 1, 2, 3.
}

🛠 4 Text Utilities

4.1 Map to String (MapToString)

Converts a map with primitive keys to a formatted string.

options := map[string]any{
    "host": "localhost",
    "port": 8080,
    "ssl":  true,
}

str := sf.MapToString(&options, "{key} = {value}", "\n")
// Possible output (key order is not guaranteed):
// host = localhost
// port = 8080
// ssl = true
4.2 Slice to String (SliceToString, SliceSameTypeToString)

Converts slices to a string using a specified separator.

// For a slice of any type
mixedSlice := []any{100, "text", 3.14}
separator := " | "
result1 := sf.SliceToString(&mixedSlice, &separator)
// result1: "100 | text | 3.14"

// For a typed slice
numSlice := []int{10, 20, 30}
result2 := sf.SliceSameTypeToString(&numSlice, &separator)
// result2: "10 | 20 | 30"

📊 5 Benchmarks

The library is optimized for high-load scenarios. Key benchmarks show significant performance gains (performance could be differ due to 1. different CPU architectures 2. statistics):

Formatting (Format) vs fmt.Sprintf: 3-5x faster for complex templates. Slices (SliceToString) vs manual fmt-based joining: from 2.5 faster up to 20 items.

Run the benchmarks yourself:

go test -bench=Format -benchmem -cpu 1
go test -bench=Fmt -benchmem -cpu 1
go test -bench=MapToStr -benchmem -cpu 1

Some benchmark screenshots:

  1. Format and FormatComplex: Format

  2. MapToStr: MapToStr benchmarks

  3. SliceToStr: SliceToStr benchmarks

📄 6 License

This project is licensed under the MIT License - see the LICENSE file for details.

🤝 7 Contributing

Contributions are welcome! If you find a bug or have a feature suggestion, please open an issue or submit a pull request.

Contributors:

Documentation

Index

Constants

View Source
const (
	// KeyKey placeholder will be formatted to map key
	KeyKey = "key"
	// KeyValue placeholder will be formatted to map value
	KeyValue = "value"
)

Variables

This section is empty.

Functions

func Format

func Format(template string, args ...any) string

Format

Func that makes string formatting from template
* It differs from above function only by generic interface that allow to use only primitive data types:
* - integers (int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uin64)
* - floats (float32, float64)
* - boolean
* - string
* - complex
* - objects
* This function defines format automatically
* Parameters
*    - template - string that contains template
*    - args - values that are using for formatting with template
* Returns formatted string

func FormatComplex

func FormatComplex(template string, args map[string]any) string

FormatComplex

Function that format text using more complex templates contains string literals i.e "Hello {username} here is our application {appname}"
* Parameters
*    - template - string that contains template
*    - args - values (dictionary: string key - any value) that are using for formatting with template
* Returns formatted string

func GetFormattingStyleOptions added in v1.6.0

func GetFormattingStyleOptions(style string) (FormattingStyle, CaseSetting, CaseSetting)

GetFormattingStyleOptions function that defines formatting style, case of first char and result from string

*

func MapToString added in v1.1.0

func MapToString[
	K string | int | uint | int32 | int64 | uint32 | uint64,
	V any,
](data map[K]V, format string, separator string) string

MapToString - format map keys and values according to format, joining parts with separator. Format should contain key and value placeholders which will be used for formatting, e.g. "{key} : {value}", or "{value}", or "{key} => {value}". Parts order in resulting string is not guranteed.

func SetStyle added in v1.6.0

func SetStyle(text *string, style FormattingStyle, firstSymbol CaseSetting, textCase CaseSetting) string

SetStyle is a function that converts text with code to defined code style.

Set text like a code style to on from FormattingStyle (Camel, Snake, or Kebab)
* conversion of abbreviations like JSON, USB, and so on is going like a regular text
* for current version, therefore they these abbreviations could be in a different
* case after conversion.
* Case settings apply in the following order : 1 - textCase, 2 - firstSymbol.
* If you are not applying textCase to text converting from Camel to Snake or Kebab
* result is lower case styled text. textCase does not apply to Camel style.
* Parameters:
* - text - pointer to text
* - style - new code style
* - firstSymbol - case settings for first symbol
* - textCase - case settings for whole text except first symbol
* Returns : new string with formatted line

func SliceSameTypeToString added in v1.4.1

func SliceSameTypeToString[T any](data *[]T, separator *string) string

func SliceToString added in v1.4.0

func SliceToString(data *[]any, separator *string) string

SliceToString function that converts slice of any type items to string in format {item}{sep}{item}... TODO(UMV): probably add one more param to wrap item in quotes if necessary

Types

type CaseSetting added in v1.6.0

type CaseSetting int
const (
	ToUpper   CaseSetting = 1
	ToLower   CaseSetting = 2
	NoChanges CaseSetting = 3
)

type FormattingStyle added in v1.6.0

type FormattingStyle string
const (
	Camel FormattingStyle = "camel"
	Snake FormattingStyle = "snake"
	Kebab FormattingStyle = "kebab"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL