Skip to content
goloopPublic

About

Package slug generate slug from Unicode string.

Topics

Resources

Code of conduct

Contributing

Stars

5 stars

Watchers

1 watching

Forks

Repository files navigation

deps.dev License License Stay with Ukraine

slug

slug generates URL-friendly slugs from Unicode strings in any language. It offers simple package-level functions and a configurable, immutable Slug type built from functional options.

Its guiding rule is that word boundaries are never silently lost: a punctuation mark separates words instead of gluing them together, and a handful of meaningful symbols become their own words (@ → at, & → and). The result stays readable, not a run-together blob.

Features

  • Clean, URL-safe slug generation from any Unicode text.
  • Multi-language transliteration.
  • Punctuation and symbols handled predictably (never silently merges words).
  • Configurable separator, maximum length and empty-input fallback.
  • Uniqueness helper and slug validation.
  • Immutable and safe for concurrent use.

Installation

go get github.com/goloop/slug/v2
import "github.com/goloop/slug/v2"

Requires Go 1.24 or newer.

Quick start

package main

import (
    "fmt"

    "github.com/goloop/slug/v2"
)

func main() {
    fmt.Println(slug.Make("Hello World"))    // Hello-World
    fmt.Println(slug.MakeURL("Hello World")) // hello-world
    fmt.Println(slug.Lower("Hello World"))   // hello-world
    fmt.Println(slug.Upper("Hello World"))   // HELLO-WORLD

    // Make keeps the case transliteration produced, so a non-Latin title
    // comes back capitalised. Use MakeURL when the slug goes into a URL.
    fmt.Println(slug.Make("Осінній настрій"))    // Osinnii-nastrii
    fmt.Println(slug.MakeURL("Осінній настрій")) // osinnii-nastrii

    // Punctuation separates words; it is never dropped silently.
    fmt.Println(slug.Make("co-operate"))     // co-operate
    fmt.Println(slug.Make("email@site.com")) // email-at-site-com
    fmt.Println(slug.Make("R&D 100%"))       // R-and-D-100-pct
}

Build a configured, immutable maker for full control:

import "github.com/goloop/slug/v2/lang"

s := slug.New(
    slug.WithLang(lang.UK),    // regional transliteration
    slug.WithSeparator("_"),   // custom word separator (default "-")
    slug.WithMaxLength(60),    // cut on a word boundary
    slug.WithFallback("post"), // value for an empty result
)

s.Make("Привіт, світ!") // Pryvit_svit

Documentation

Contributing

Contributions are welcome. Please run go test ./..., go vet ./... and gofmt -l . before submitting a pull request.

License

slug is released under the MIT License. See LICENSE.

Release v2.4.1

Limit slug buffer allocation to the requested output length.

About

Package slug generate slug from Unicode string.

Topics

Resources

Code of conduct

Contributing

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages