Thanks to visit codestin.com
Credit goes to pkg.go.dev

stringish

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2025 License: MIT Imports: 0 Imported by: 1

README

stringish

A small Go module that provides a generic type constraint for “string-like” data, and a utf8 package that works with both strings and byte slices without conversions.

type Interface interface {
	~[]byte | ~string
}

Go Reference Test Status

Install

go get github.com/clipperhouse/stringish

Examples

import (
    "github.com/clipperhouse/stringish"
    "github.com/clipperhouse/stringish/utf8"
)

s := "Hello, 世界"
r, size := utf8.DecodeRune(s)   // not DecodeRuneInString 🎉

b := []byte("Hello, 世界")
r, size = utf8.DecodeRune(b)    // same API!

func MyFoo[T stringish.Interface](s T) T {
    // pass a string or a []byte
    // iterate, slice, transform, whatever
}

Motivation

Sometimes we want APIs to accept string or []byte without having to convert between those types. That conversion usually allocates!

By implementing with stringish.Interface, we can have a single API, and single implementation for both types: one Foo instead of Foo and FooString.

We have converted the unicode/utf8 package as an example -- note the absence of*InString funcs. We might look at x/text next.

Used by

Prior discussion

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interface

type Interface interface {
	~[]byte | ~string
}

Directories

Path Synopsis
Package utf8 implements functions and constants to support text encoded in UTF-8.
Package utf8 implements functions and constants to support text encoded in UTF-8.

Jump to

Keyboard shortcuts

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