go124

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2025 License: MIT Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExampleCleanup

func ExampleCleanup()

ExampleCleanup demonstrates resource cleanup patterns.

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

func Filter[T any](seq iter.Seq[T], predicate func(T) bool) iter.Seq[T]

Filter returns an iterator that yields only values satisfying the predicate. This shows functional composition with iterators.

func Map

func Map[T, U any](seq iter.Seq[T], fn func(T) U) iter.Seq[U]

Map transforms values from the source iterator using the given function.

func Range

func Range(start, end int) iter.Seq[int]

Range returns an iterator that generates integers from start to end (exclusive). Demonstrates creating custom numeric sequences 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.

func (*FileHandle) Read

func (fh *FileHandle) Read() []byte

Read simulates reading from the file.

type KVMap

type KVMap[K comparable, V any] = map[K]V

KVMap is a generic type alias for maps.

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.

func None

func None[T any]() Optional[T]

None creates an empty Optional.

func Some

func Some[T any](v T) Optional[T]

Some creates an Optional with a value.

func (Optional[T]) Get

func (o Optional[T]) Get() T

Get returns the value or zero value if not present.

func (Optional[T]) IsPresent

func (o Optional[T]) IsPresent() bool

IsPresent returns true if the Optional has a value.

type OptionalSlice

type OptionalSlice[T any] = []Optional[T]

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.

func NewPair

func NewPair[A, B any](a A, b B) Pair[A, B]

NewPair creates a new pair.

func Transform

func Transform[A, B, C, D any](
	p Pair[A, B],
	f func(A) C,
	g func(B) D,
) Pair[C, D]

Transform applies functions to both elements.

func (Pair[A, B]) Swap

func (p Pair[A, B]) Swap() Pair[B, A]

Swap returns a new pair with swapped values.

type PairList

type PairList[A, B any] = []Pair[A, B]

PairList is a type alias for lists of pairs.

type Resource

type Resource struct {
	ID   string
	Data []byte
}

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

func NewResource(id string, size int) *Resource

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.

func (*Resource) Use

func (r *Resource) Use()

Use simulates using the resource.

type Result

type Result[T any] struct {
	Value T
	Error error
}

Result represents a computation result or error.

type ResultSlice

type ResultSlice[T any] = []Result[T]

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.

func (*StringCache) Get

func (sc *StringCache) Get(handle unique.Handle[string]) string

Get retrieves the original value from a handle.

func (*StringCache) Intern

func (sc *StringCache) Intern(s string) unique.Handle[string]

Intern adds a string to the cache, returning its unique handle. Multiple calls with equal strings return the same underlying memory.

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.

func (*TempBuffer) Write

func (tb *TempBuffer) Write(data []byte)

Write adds data to the buffer.

type TreeNode

type TreeNode struct {
	Value int
	Left  *TreeNode
	Right *TreeNode
}

TreeNode represents a node in a binary tree.

func (*TreeNode) InOrder

func (t *TreeNode) InOrder() iter.Seq[int]

InOrder returns an iterator that traverses the tree in-order. This demonstrates Go 1.24's iterator functions in for-range loops.

Why? Iterator functions allow custom iteration patterns without materializing collections, enabling memory-efficient tree traversals.

func (*TreeNode) PreOrder

func (t *TreeNode) PreOrder() iter.Seq[int]

PreOrder returns an iterator for pre-order traversal.

Jump to

Keyboard shortcuts

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