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). These wrappers do not change semantics.
Convenience helpers ¶
This package also provides helpers not present in the standard library:
IsEmpty / IsAnyEmpty: common emptiness checks.
Join and Concat: Join accepts variadic strings so callers can avoid allocating a slice at the callsite. Concat concatenates without a separator.
CutColon: a small helper that splits on the first ":" using strings.Cut, returning the part before and after. If ":" is not present, the "after" return value is empty.
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
- func Bytes(s string) []byte
- func Concat(ss ...string) string
- func Contains(s, substr string) bool
- func Count(s, substr string) int
- func Cut(s, sep string) (string, string, bool)
- func CutColon(s string) (string, string)
- func HasPrefix(s, prefix string) bool
- func IsAnyEmpty(ss ...string) bool
- func IsEmpty(s string) bool
- func Join(sep string, ss ...string) string
- func LastIndex(s, substr string) int
- func Repeat(s string, count int) string
- func ReplaceAll(s, o, n string) string
- func ToLower(s string) string
- func Trim(s, cutset string) string
- func TrimSpace(s string) string
- type Reader
Constants ¶
const Empty = ""
Empty is the empty string constant.
It is provided as a named constant for readability and reuse.
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 ¶
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 Contains ¶
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
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 ¶
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 CutColon ¶ added in v2.12.0
CutColon splits s on the first ":" and returns the parts before and after.
If ":" is not present, the returned after value is empty.
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 ¶
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
IsAnyEmpty reports whether any of ss are empty.
It returns true when at least one element of ss has length 0.
func IsEmpty ¶
IsEmpty reports whether s is empty.
This is equivalent to len(s) == 0, but provided for readability at call sites.
func Join ¶
Join joins ss with sep.
This is equivalent to strings.Join, but it accepts variadic input so callers can avoid allocating a slice at the callsite when they already have discrete string values.
func LastIndex ¶ added in v2.303.7
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 ¶
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
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 ToLower ¶
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.