Documentation
¶
Overview ¶
Package go124 provides examples and demonstrations of features introduced in Go 1.24 (released February 2025).
This package covers:
- Iterator functions for custom iteration patterns (iter.Seq)
- Value canonicalization with unique.Handle
- Resource cleanup with runtime.AddCleanup
- Parameterized type aliases for generic types
- Enhanced testing benchmarks with testing.B.Loop
Each file contains focused examples with godoc comments explaining the "why" behind each feature and demonstrating idiomatic usage.
Example usage:
import "github.com/KrystianMarek/golang-202/pkg/go124"
func main() {
go124.ExampleIterators()
go124.ExampleUnique()
go124.ExampleCleanup()
go124.ExampleGenericAliases()
}
Package go124 demonstrates features introduced in Go 1.24 (released February 2025).
Index ¶
- func ExampleCleanup()
- func ExampleGenericAliases()
- func ExampleIterators()
- func ExampleUnique()
- func Filter[T any](seq iter.Seq[T], predicate func(T) bool) iter.Seq[T]
- func Map[T, U any](seq iter.Seq[T], fn func(T) U) iter.Seq[U]
- func Range(start, end int) iter.Seq[int]
- type FileHandle
- type KVMap
- type KeyValue
- type LogAggregator
- type LogEntry
- type Optional
- type OptionalSlice
- type OrderedSlice
- type Pair
- type PairList
- type Resource
- type Result
- type ResultSlice
- type StringCache
- type TempBuffer
- type TreeNode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExampleGenericAliases ¶
func ExampleGenericAliases()
ExampleGenericAliases demonstrates parameterized type aliases.
func ExampleIterators ¶
func ExampleIterators()
ExampleIterators demonstrates iterator usage with Go 1.24's for-range support.
func ExampleUnique ¶
func ExampleUnique()
ExampleUnique demonstrates unique.Handle for value canonicalization.
func Filter ¶
Filter returns an iterator that yields only values satisfying the predicate. This shows functional composition with iterators.
Types ¶
type FileHandle ¶
type FileHandle struct {
Path string
// contains filtered or unexported fields
}
FileHandle represents a managed file handle.
func OpenFile ¶
func OpenFile(path string) *FileHandle
OpenFile simulates opening a file with cleanup.
type KeyValue ¶
type KeyValue[K comparable, V any] struct { Key K Value V }
KeyValue represents a generic key-value pair.
type LogAggregator ¶
type LogAggregator struct {
// contains filtered or unexported fields
}
LogAggregator collects log entries with string interning.
func NewLogAggregator ¶
func NewLogAggregator() *LogAggregator
NewLogAggregator creates a new log aggregator.
func (*LogAggregator) AddLog ¶
func (la *LogAggregator) AddLog(level, message, source string)
AddLog adds a log entry with automatic string interning.
func (*LogAggregator) GetLogs ¶
func (la *LogAggregator) GetLogs() []string
GetLogs returns all log entries as strings.
type LogEntry ¶
type LogEntry struct {
Level unique.Handle[string]
Message unique.Handle[string]
Source unique.Handle[string]
}
LogEntry represents a structured log entry with interned strings to reduce memory footprint.
type Optional ¶
type Optional[T any] struct { // contains filtered or unexported fields }
Optional represents an optional value.
type OptionalSlice ¶
OptionalSlice is a type alias for slices of optionals.
type OrderedSlice ¶
type OrderedSlice[T comparable] = []T
OrderedSlice is a parameterized type alias for slices of ordered types. Go 1.24 allows generic type aliases, enabling concise type definitions.
Why? Generic type aliases reduce boilerplate and improve readability when working with complex generic types.
type Pair ¶
type Pair[A, B any] struct { First A Second B }
Pair represents a generic pair of values.
type Resource ¶
Resource represents a managed resource with cleanup. This demonstrates resource cleanup patterns.
Why? Automatic cleanup using finalizers helps prevent resource leaks. While SetFinalizer is used here, Go 1.24 introduces runtime.AddCleanup for more predictable cleanup behavior. This is useful for file handles, network connections, and temporary resources.
func NewResource ¶
NewResource creates a resource with automatic cleanup. Note: runtime.AddCleanup is a Go 1.24 feature. If not available, consider using runtime.SetFinalizer as an alternative.
type ResultSlice ¶
ResultSlice is a type alias for slices of results.
type StringCache ¶
type StringCache struct {
// contains filtered or unexported fields
}
StringCache demonstrates using unique.Handle for value canonicalization. This is useful for deduplicating immutable values in memory-intensive applications.
Why? The unique package allows interning values, ensuring only one copy exists in memory. This is particularly useful for string-heavy workloads like log processing, configuration management, or compiler symbol tables.
func NewStringCache ¶
func NewStringCache() *StringCache
NewStringCache creates a new cache for string interning.
type TempBuffer ¶
type TempBuffer struct {
Name string
// contains filtered or unexported fields
}
TempBuffer represents a temporary buffer with automatic cleanup.
func NewTempBuffer ¶
func NewTempBuffer(name string, capacity int) *TempBuffer
NewTempBuffer creates a temporary buffer.
type TreeNode ¶
TreeNode represents a node in a binary tree.