strings

package
v2.679.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package strings provides small string helpers and a curated set of aliases for the Go standard library strings package.

The intent of this package is to:

  • Keep go-service code depending on go-service packages consistently, while still delegating to the standard library for core string operations.
  • Provide a few convenience helpers that are frequently needed across the repository (for example emptiness checks and small splitting helpers).
  • Expose carefully documented unsafe utilities (see Bytes) for performance- sensitive paths where avoiding allocations is important.

Most functions in this package are thin wrappers around the corresponding functions in the standard library strings package (for example Contains, HasPrefix, ReplaceAll, TrimSpace) or the strcase package (for example ToSnake, ToLowerCamel, ToDelimited). These wrappers do not change semantics.

Convenience helpers

This package also provides helpers not present in the standard library:

  • IsEmpty / IsAnyEmpty: common emptiness checks. IsAnyEmpty returns false when called without values.

  • Join and Concat: Join takes the separator first and then variadic strings, so callers can avoid allocating a slice at the call site. With no strings it returns Empty. Concat concatenates without a separator.

  • CutColon: a small helper that splits on the first ":" using strings.Cut, returning the part before, the part after, and whether ":" was found. If ":" is not present, "before" is the original input and "after" is empty.

  • ToSnake / ToLowerCamel / ToDelimited: common string case conversion helpers delegated to strcase.

Constants

Empty and Space are provided as named constants for readability and reuse.

Unsafe conversions

Bytes converts a string to a byte slice without allocation by using unsafe.

Important: the returned []byte aliases the same memory as the input string.

  • Treat the returned slice as read-only. Writing to it results in undefined behavior.

  • Do not retain the returned slice beyond the lifetime of the original string value. In particular, do not store it in long-lived structures or return it when the string was derived from a transient buffer.

For safe conversions, use the built-in conversion []byte(s), which allocates a new byte slice.

Index

Constants

View Source
const Empty = ""

Empty is the empty string constant.

It is provided as a named constant for readability and reuse.

View Source
const Space = " "

Space is the single ASCII space character (" ").

It is provided as a named constant for readability and reuse.

Variables

This section is empty.

Functions

func Bytes

func Bytes(s string) []byte

Bytes converts s to a []byte without allocating.

This function uses unsafe to create a byte slice view over the string's backing storage. As a result:

  • The returned slice aliases the same memory as s.
  • The returned slice must be treated as read-only. Writing to it results in undefined behavior.
  • Do not retain the returned slice beyond the lifetime of s. In particular, do not store it in long-lived structures or return it when s was derived from a transient buffer.

For a safe conversion that does not alias the input string, use []byte(s), which allocates a new slice.

func Concat

func Concat(ss ...string) string

Concat concatenates ss without a separator.

This is equivalent to Join(Empty, ss...).

func Contains

func Contains(s, substr string) bool

Contains reports whether substr is within s.

This is a thin wrapper around strings.Contains and does not change semantics.

func Count added in v2.194.0

func Count(s, substr string) int

Count counts the number of non-overlapping instances of substr in s.

This is a thin wrapper around strings.Count and does not change semantics.

func Cut

func Cut(s, sep string) (string, string, bool)

Cut slices s around the first instance of sep, returning the text before and after sep.

The returned boolean reports whether sep was found.

This is a thin wrapper around strings.Cut and does not change semantics.

func CutAfter added in v2.643.0

func CutAfter(s, sep string) string

CutAfter slices s around the first instance of sep and returns the text after sep.

If sep is not present, it returns Empty.

func CutColon added in v2.12.0

func CutColon(s string) (string, string, bool)

CutColon splits s on the first ":" and returns the parts before and after.

If ":" is not present, before is the original input and after is Empty. The returned boolean reports whether ":" was found.

This helper is commonly used by go-service "source string" conventions where a value is prefixed with a kind such as "env:NAME" or "file:/path".

func HasPrefix

func HasPrefix(s, prefix string) bool

HasPrefix reports whether s begins with prefix.

This is a thin wrapper around strings.HasPrefix and does not change semantics.

func IsAnyEmpty added in v2.24.0

func IsAnyEmpty(ss ...string) bool

IsAnyEmpty reports whether any of ss are empty.

It returns true when at least one element of ss has length 0. With no values, it returns false.

func IsEmpty

func IsEmpty(s string) bool

IsEmpty reports whether s is empty.

This is equivalent to len(s) == 0, but provided for readability at call sites.

func Join

func Join(sep string, ss ...string) string

Join joins ss with sep, taking the separator before the values to join.

This is equivalent to strings.Join(ss, sep), but it accepts variadic input so callers can avoid allocating a slice at the call site when they already have discrete string values. With no values, it returns Empty.

func LastIndex added in v2.303.7

func LastIndex(s, substr string) int

LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.

This is a thin wrapper around strings.LastIndex and does not change semantics.

func Repeat

func Repeat(s string, count int) string

Repeat returns a new string consisting of count copies of s.

This is a thin wrapper around strings.Repeat and does not change semantics.

func ReplaceAll added in v2.93.0

func ReplaceAll(s, o, n string) string

ReplaceAll returns a copy of s with all non-overlapping instances of o replaced by n.

This is a thin wrapper around strings.ReplaceAll and does not change semantics.

func ToDelimited added in v2.412.0

func ToDelimited(s string, delimiter uint8) string

ToDelimited converts s to a delimiter-separated string.

This is a thin wrapper around strcase.ToDelimited and does not change semantics.

func ToLower

func ToLower(s string) string

ToLower returns s with all Unicode letters mapped to their lower case.

This is a thin wrapper around strings.ToLower and does not change semantics.

func ToLowerCamel added in v2.412.0

func ToLowerCamel(s string) string

ToLowerCamel converts s to lower camel case.

This is a thin wrapper around strcase.ToLowerCamel and does not change semantics.

func ToSnake added in v2.412.0

func ToSnake(s string) string

ToSnake converts s to snake case.

This is a thin wrapper around strcase.ToSnake and does not change semantics.

func Trim added in v2.303.7

func Trim(s, cutset string) string

Trim returns a slice of s with all leading and trailing Unicode code points contained in cutset removed.

This is a thin wrapper around strings.Trim and does not change semantics.

func TrimSpace

func TrimSpace(s string) string

TrimSpace returns s with all leading and trailing white space removed.

White space is defined by Unicode.

This is a thin wrapper around strings.TrimSpace and does not change semantics.

Types

type Reader added in v2.331.0

type Reader = strings.Reader

Reader is an alias of strings.Reader.

It is provided so go-service code can depend on a consistent import path while preserving standard library semantics.

func NewReader added in v2.331.0

func NewReader(s string) *Reader

NewReader returns a new Reader reading from s.

This is a thin wrapper around strings.NewReader and does not change semantics.

Jump to

Keyboard shortcuts

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