Documentation
¶
Overview ¶
Package common provides shared utilities and helpers for protomcp.org projects.
The common package serves as a foundation library for the protomcp.org ecosystem, preventing code duplication across repositories like nanorpc and protomcp. It provides general-purpose utilities, test helpers, and patterns that are shared across multiple projects.
Slice Utilities ¶
The package provides generic slice manipulation functions that help prevent memory leaks when working with slices containing pointers or reference types:
- ClearSlice: Zeros all elements including unused capacity and returns an empty slice that reuses the underlying array
- ClearAndNilSlice: Zeros all elements and returns nil, releasing the underlying array for garbage collection
These utilities are particularly useful when truncating slices that contain pointers, interfaces, or other reference types, as they ensure no references remain in the unused capacity that could prevent garbage collection.
Example usage:
// Prevent memory leaks when clearing a slice of pointers
handlers := []*Handler{h1, h2, h3}
handlers = ClearSlice(handlers) // All pointers nil, slice empty but reusable
// Or completely release the underlying array
responses := []Response{{...}, {...}, {...}}
responses = ClearAndNilSlice(responses) // All cleared, slice is nil
String Building ¶
LazyBuffer provides a convenient wrapper around strings.Builder that eliminates the need for error handling. It's particularly useful for code generation, template rendering, or any scenario where you're building strings incrementally:
- WriteString: Appends strings, ignoring empty ones
- WriteRunes: Appends individual runes
- Printf: Appends formatted strings
- All methods support chaining and nil-safety
Example usage:
var buf LazyBuffer
output := buf.WriteString("func ").
WriteString(name).
WriteRunes('(').
Printf("ctx %s", contextType).
WriteRunes(')').
String()
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClearAndNilSlice ¶
func ClearAndNilSlice[T any](s []T) []T
ClearAndNilSlice zeros all elements in a slice (including unused capacity) and returns nil. This completely releases the underlying array for garbage collection.
Example:
responses := []Response{{...}, {...}, {...}}
responses = ClearAndNilSlice(responses) // All elements zeroed, slice is nil
func ClearSlice ¶
func ClearSlice[T any](s []T) []T
ClearSlice zeros all elements in a slice (including unused capacity) and returns an empty slice that reuses the same underlying array. This prevents memory leaks when truncating slices containing pointers or other reference types.
Example:
responses := []Response{{...}, {...}, {...}}
responses = ClearSlice(responses) // All elements zeroed, length is 0
Types ¶
type LazyBuffer ¶
LazyBuffer is a wrapper around strings.Builder that provides convenience methods for building output strings without requiring error handling. Unlike strings.Builder, LazyBuffer methods don't return errors, making it "lazy" - you can chain calls without `_, _ =` error handling. It also handles nil receivers gracefully.
LazyBuffer is particularly useful in code generation, template rendering, or any scenario where you're building strings incrementally and error handling would clutter the code without providing value (since strings.Builder methods only return errors on out-of-memory conditions).
Example:
var buf LazyBuffer
output := buf.WriteString("Hello").
WriteRunes(' ').
WriteString("world").
Printf(", %d", 42).
WriteRunes('!').
String()
// output: "Hello world, 42!"
func (*LazyBuffer) Len ¶
func (b *LazyBuffer) Len() int
Len returns the number of accumulated bytes. Returns 0 if receiver is nil.
func (*LazyBuffer) Printf ¶
func (b *LazyBuffer) Printf(format string, args ...any) *LazyBuffer
Printf appends a formatted string to the buffer using fmt.Fprintf. Safe to call on nil receiver. Returns the buffer for method chaining.
func (*LazyBuffer) Reset ¶
func (b *LazyBuffer) Reset()
Reset resets the buffer to be empty. Safe to call on nil receiver.
func (*LazyBuffer) String ¶
func (b *LazyBuffer) String() string
String returns the accumulated string. Returns empty string if receiver is nil.
func (*LazyBuffer) WriteRunes ¶
func (b *LazyBuffer) WriteRunes(rr ...rune) *LazyBuffer
WriteRunes appends one or more runes to the buffer. Safe to call on nil receiver. Returns the buffer for method chaining.
func (*LazyBuffer) WriteString ¶
func (b *LazyBuffer) WriteString(ss ...string) *LazyBuffer
WriteString appends one or more strings to the buffer, ignoring empty strings. Safe to call on nil receiver. Returns the buffer for method chaining.