### Implement Re-armable Once with Error Handling using syncutil.Once in Go Source: https://context7.com/go4org/go4/llms.txt The syncutil.Once package in Go provides a re-armable 'Once' mechanism with error handling, unlike the standard sync.Once. It retries initialization until it succeeds, making it suitable for operations that might fail transiently. This example simulates an initialization that fails twice before succeeding. ```go package main import ( "errors" "fmt" "sync/atomic" "go4.org/syncutil" ) func main() { var once syncutil.Once var attempts int32 // Simulates an initialization that fails twice then succeeds initFunc := func() error { n := atomic.AddInt32(&attempts, 1) if n < 3 { return errors.New("connection failed") } fmt.Println("Successfully initialized on attempt", n) return nil } // Multiple goroutines trying to initialize for i := 0; i < 5; i++ { if err := once.Do(initFunc); err != nil { fmt.Printf("Init attempt %d failed: %v\n", i+1, err) } else { fmt.Printf("Init attempt %d succeeded\n", i+1) } } // Output: Retries until success, then returns nil for all subsequent calls } ``` -------------------------------- ### Implement XDG Base Directory Specification in Go Source: https://context7.com/go4org/go4/llms.txt The xdgdir package implements the Free Desktop Base Directory specification. It provides functions to get configuration, data, cache, and runtime directory paths, as well as search paths. It also allows creating and opening files within these directories and retrieving environment variable names. ```go package main import ( "fmt" "log" "go4.org/xdgdir" ) func main() { // Get XDG directories fmt.Printf("Config dir: %s\n", xdgdir.Config.Path()) // ~/.config fmt.Printf("Data dir: %s\n", xdgdir.Data.Path()) // ~/.local/share fmt.Printf("Cache dir: %s\n", xdgdir.Cache.Path()) // ~/.cache fmt.Printf("Runtime dir: %s\n", xdgdir.Runtime.Path()) // /run/user/1000 // Get search paths (includes system directories) fmt.Printf("Config search paths: %v\n", xdgdir.Config.SearchPaths()) // Output: [~/.config /etc/xdg] fmt.Printf("Data search paths: %v\n", xdgdir.Data.SearchPaths()) // Output: [~/.local/share /usr/local/share /usr/share] // Create config file (creates parent dirs with mode 0700) f, err := xdgdir.Config.Create("myapp/settings.json") if err != nil { log.Fatal(err) } f.Write([]byte(`{"theme": "dark"}`)) f.Close() fmt.Println("Created config file") // Open file (searches all paths) configFile, err := xdgdir.Config.Open("myapp/settings.json") if err != nil { log.Printf("Config not found: %v", err) } else { configFile.Close() fmt.Println("Found config file") } // Environment variable names fmt.Printf("Config env var: %s\n", xdgdir.Config.String()) // XDG_CONFIG_HOME fmt.Printf("Data env var: %s\n", xdgdir.Data.String()) // XDG_DATA_HOME } ``` -------------------------------- ### Limit Concurrency with syncutil.Gate in Go Source: https://context7.com/go4org/go4/llms.txt The syncutil.Gate package in Go provides a mechanism to limit the number of concurrent operations. It's useful for controlling resource usage like database connections or API rate limits. This example demonstrates creating a gate that allows a maximum of 3 concurrent operations. ```go package main import ( "fmt" "sync" "time" "go4.org/syncutil" ) func main() { // Create a gate allowing max 3 concurrent operations gate := syncutil.NewGate(3) var wg sync.WaitGroup for i := 0; i < 10; i++ { wg.Add(1) go func(id int) { defer wg.Done() gate.Start() // Block until slot available defer gate.Done() fmt.Printf("Worker %d: processing\n", id) time.Sleep(100 * time.Millisecond) fmt.Printf("Worker %d: done\n", id) }(i) } wg.Wait() // Output: Only 3 workers run concurrently at any time } ``` -------------------------------- ### Collect Errors with syncutil.Group WaitGroup in Go Source: https://context7.com/go4org/go4/llms.txt The syncutil.Group package in Go provides an error-collecting WaitGroup to coordinate multiple goroutines and aggregate their errors. This offers a cleaner alternative to manual error aggregation compared to the standard sync.WaitGroup. The example shows fetching multiple URLs and collecting any errors encountered. ```go package main import ( "errors" "fmt" "net/http" "go4.org/syncutil" ) func main() { var g syncutil.Group urls := []string{ "https://example.com", "https://invalid.example.com", "https://httpbin.org/get", } for _, url := range urls { url := url // capture variable g.Go(func() error { resp, err := http.Get(url) if err != nil { return fmt.Errorf("fetch %s: %w", url, err) } defer resp.Body.Close() fmt.Printf("Fetched %s: %d\n", url, resp.StatusCode) return nil }) } // Wait and get first error (or nil) if err := g.Err(); err != nil { fmt.Printf("First error: %v\n", err) } // Or get all errors for _, err := range g.Errs() { fmt.Printf("Error: %v\n", err) } } ``` -------------------------------- ### Associate HTTP Clients with Contexts in Go using ctxutil Source: https://context7.com/go4org/go4/llms.txt The ctxutil package provides utilities for context management, particularly for associating custom `http.Client` instances with contexts. The `ctxutil.Client` function retrieves an HTTP client from a context, falling back to `http.DefaultClient` if none is found or if the context is nil. ```go package main import ( "fmt" "net/http" "go4.org/ctxutil" "golang.org/x/net/context" ) func main() { // Create custom HTTP client customClient := &http.Client{ Timeout: 30, } // Associate client with context ctx := context.WithValue(context.Background(), ctxutil.HTTPClient, customClient) // Retrieve client from context (or get default) client := ctxutil.Client(ctx) fmt.Printf("Got client: %v\n", client == customClient) // true // With nil context, returns http.DefaultClient defaultClient := ctxutil.Client(nil) fmt.Printf("Is default: %v\n", defaultClient == http.DefaultClient) // true } ``` -------------------------------- ### Simulate Network Throttling with go4/net/throttle Source: https://context7.com/go4org/go4/llms.txt This Go code snippet shows how to use the 'go4.org/net/throttle' package to create a network listener that simulates slow network conditions. It wraps a standard 'net.Listener' and applies configurable download and upload rates, as well as latency. This is useful for testing applications under realistic network constraints. ```go package main import ( "fmt" "io" "net" "net/http" "time" "go4.org/net/throttle" ) func main() { // Create underlying listener ln, err := net.Listen("tcp", "localhost:0") if err != nil { panic(err) } // Wrap with throttling throttledLn := &throttle.Listener{ Listener: ln, Down: throttle.Rate{ KBps: 100, // 100 KB/s download Latency: 50 * time.Millisecond, }, Up: throttle.Rate{ KBps: 50, // 50 KB/s upload Latency: 100 * time.Millisecond, }, } // Use with HTTP server server := &http.Server{ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Read request body (throttled) body, _ := io.ReadAll(r.Body) fmt.Printf("Received: %s\n", body) // Write response (throttled) w.Write([]byte("Hello from throttled server!")) }), } go server.Serve(throttledLn) // Client requests will experience artificial latency and bandwidth limits addr := throttledLn.Addr().String() fmt.Printf("Throttled server running at http://%s\n", addr) // Make a test request start := time.Now() resp, err := http.Get("http://" + addr) if err == nil { io.ReadAll(resp.Body) resp.Body.Close() fmt.Printf("Request took: %v (with throttling)\n", time.Since(start)) } } ``` -------------------------------- ### Reader Utilities: Stats, Buffering, and Fake Seeking (Go) Source: https://context7.com/go4org/go4/llms.txt The readerutil package provides utility wrappers for io.Reader, including a StatsReader for tracking bytes read (integrating with expvar), a BufferingReaderAt for random access from sequential readers, and a FakeSeeker that simulates seeking capabilities. ```go package main import ( "bytes" "expvar" "fmt" "io" "strings" "go4.org/readerutil" ) func main() { // Track bytes read with expvar bytesRead := expvar.NewInt("bytes_read") reader := strings.NewReader("Hello, World! This is test data.") statsReader := readerutil.NewStatsReader(bytesRead, reader) buf := make([]byte, 10) for { n, err := statsReader.Read(buf) if n > 0 { fmt.Printf("Read: %q\n", buf[:n]) } if err == io.EOF { break } } fmt.Printf("Total bytes read: %s\n", bytesRead.String()) // BufferingReaderAt - enables random access from sequential reader seqReader := strings.NewReader("Random access content here") readerAt := readerutil.NewBufferingReaderAt(seqReader) buf2 := make([]byte, 6) readerAt.ReadAt(buf2, 7) // Read "access" starting at offset 7 fmt.Printf("Read at offset 7: %q\n", buf2) // FakeSeeker - wrapper that pretends to support seeking content := "Sequential data that looks seekable" fakeSeeker := readerutil.NewFakeSeeker( strings.NewReader(content), int64(len(content)), ) // Can seek to end to get size size, _ := fakeSeeker.Seek(0, io.SeekEnd) fmt.Printf("Content size: %d\n", size) // Seek back to start for reading fakeSeeker.Seek(0, io.SeekStart) } ``` -------------------------------- ### Filesystem Abstraction: Cloud and Local Storage Access (Go) Source: https://context7.com/go4org/go4/llms.txt The wkfs package offers a pluggable filesystem abstraction, enabling uniform access to cloud storage (GCS, S3) and local files. It supports operations like reading, writing, opening files, and creating directories. ```go package main import ( "fmt" "log" "os" "go4.org/wkfs" _ "go4.org/wkfs/gcs" // Register GCS filesystem for /gcs/ paths ) func main() { // Write to local file err := wkfs.WriteFile("/tmp/local-file.txt", []byte("Hello, local!"), 0644) if err != nil { log.Fatal(err) } // Read from local file - falls through to OS data, err := wkfs.ReadFile("/tmp/local-file.txt") if err != nil { log.Fatal(err) } fmt.Printf("Local content: %s\n", data) // With GCS import, paths like /gcs/bucket/object are intercepted: // data, err := wkfs.ReadFile("/gcs/my-bucket/my-file.txt") // Open files for reading f, err := wkfs.Open("/tmp/local-file.txt") if err != nil { log.Fatal(err) } defer f.Close() info, _ := f.Stat() fmt.Printf("File size: %d bytes\n", info.Size()) // Create directories wkfs.MkdirAll("/tmp/myapp/data", 0755) // Create/write files writer, err := wkfs.Create("/tmp/myapp/data/output.txt") if err != nil { log.Fatal(err) } writer.Write([]byte("Output data")) writer.Close() } ``` -------------------------------- ### String Utilities: Case-Insensitive Matching and JSON Detection (Go) Source: https://context7.com/go4org/go4/llms.txt The strutil package provides functions for case-insensitive string comparisons (prefix, suffix, contains) and detecting if a string appears to be JSON. It also includes string interning for memory efficiency. ```go package main import ( "fmt" "go4.org/strutil" ) func main() { // Case-insensitive prefix check (Unicode-aware) fmt.Println(strutil.HasPrefixFold("Hello World", "hello")) // true fmt.Println(strutil.HasPrefixFold("GRUSS GOTT", "gruss")) // true // Case-insensitive suffix check fmt.Println(strutil.HasSuffixFold("document.PDF", ".pdf")) // true fmt.Println(strutil.HasSuffixFold("README.MD", ".md")) // true // Case-insensitive contains fmt.Println(strutil.ContainsFold("The Quick Brown Fox", "quick")) // true fmt.Println(strutil.ContainsFold("HELLO WORLD", "world")) // true // Detect if string looks like JSON fmt.Println(strutil.IsPlausibleJSON(`{"key": "value"}`)) // true fmt.Println(strutil.IsPlausibleJSON(` { "a": 1 } `)) // true (handles whitespace) fmt.Println(strutil.IsPlausibleJSON(`not json`)) // false // String interning for memory efficiency strutil.RegisterCommonString("GET", "POST", "PUT", "DELETE") method := strutil.StringFromBytes([]byte("GET")) // Returns interned string fmt.Printf("Method: %s\n", method) } ``` -------------------------------- ### Inject Faults with go4/fault for Testing Source: https://context7.com/go4org/go4/llms.txt This Go code snippet demonstrates fault injection using the 'go4.org/fault' package. It allows simulating failures in operations by configuring fault injection percentages via environment variables. The 'fault.NewInjector' creates an injector, and 'injector.FailErr' or 'injector.ShouldFail' can be used to trigger simulated errors. ```go package main import ( "fmt" "os" "go4.org/fault" ) func main() { // Set environment to inject faults 30% of the time os.Setenv("FAULT_DATABASE_FAIL_PERCENT", "30") // Create injector for database operations dbFault := fault.NewInjector("database") // Simulate multiple operations for i := 0; i < 10; i++ { err := simulateDatabaseOp(dbFault) if err != nil { fmt.Printf("Operation %d: FAILED - %v\n", i, err) } else { fmt.Printf("Operation %d: SUCCESS\n", i) } } } func simulateDatabaseOp(injector *fault.Injector) error { var err error // Check if we should inject a fault if injector.FailErr(&err) { return err // Returns fake error } // Alternative: check without setting error if injector.ShouldFail() { return fmt.Errorf("injected database failure") } // Normal operation return nil } ``` -------------------------------- ### Multi-unit Semaphore with go4.org/syncutil/Sem Source: https://context7.com/go4org/go4/llms.txt Implements a semaphore that can acquire and release multiple units at once, suitable for resource pooling. It takes the number of units as an integer and supports acquiring and releasing specified amounts of units. ```go package main import ( "fmt" "sync" "go4.org/syncutil" ) func main() { // Semaphore with 100 units (e.g., representing 100MB of memory) sem := syncutil.NewSem(100) var wg sync.WaitGroup // Simulate jobs requiring different amounts of resources jobs := []struct { name string units int64 }{ {"small", 10}, {"medium", 30}, {"large", 50}, {"huge", 80}, } for _, job := range jobs { job := job wg.Add(1) go func() { defer wg.Done() // Acquire required units (blocks if not enough available) if err := sem.Acquire(job.units); err != nil { fmt.Printf("Job %s: failed to acquire %d units: %v\n", job.name, job.units, err) return } fmt.Printf("Job %s: acquired %d units\n", job.name, job.units) // Do work... sem.Release(job.units) fmt.Printf("Job %s: released %d units\n", job.name, job.units) }() } wg.Wait() } ``` -------------------------------- ### Cross-Process File Locking with go4.org/lock Source: https://context7.com/go4org/go4/llms.txt Provides file-based locking for coordinating ownership across processes using fcntl advisory locks on Unix systems with a portable fallback. It returns a closer to release the lock. ```go package main import ( "fmt" "log" "os" "path/filepath" "go4.org/lock" ) func main() { lockPath := filepath.Join(os.TempDir(), "myapp.lock") // Acquire exclusive lock closer, err := lock.Lock(lockPath) if err != nil { log.Fatalf("Failed to acquire lock: %v", err) } defer closer.Close() fmt.Println("Lock acquired, doing exclusive work...") // Only one process can reach here at a time // Other processes attempting to Lock() will block or fail // Simulate work fmt.Println("Work complete, releasing lock") // Lock automatically released when closer.Close() is called } ``` -------------------------------- ### JSON Configuration with Validation using go4.org/jsonconfig Source: https://context7.com/go4org/go4/llms.txt Offers type-safe JSON configuration reading with support for required/optional fields, validation, includes, and environment variable expansion. It uses a map-like structure for configuration values. ```go package main import ( "fmt" "log" "go4.org/jsonconfig" ) func main() { // Sample JSON: {"host": "localhost", "port": 8080, "debug": true, "tags": ["web", "api"]} config := jsonconfig.Obj{ "host": "localhost", "port": float64(8080), "debug": true, "tags": []interface{}{"web", "api"}, } // Extract values with type safety host := config.RequiredString("host") port := config.OptionalInt("port", 3000) // default: 3000 debug := config.OptionalBool("debug", false) // default: false timeout := config.OptionalInt("timeout", 30) // not in config, uses default tags := config.OptionalList("tags") // Validate - catches unknown keys and missing required fields if err := config.Validate(); err != nil { log.Fatalf("Config validation failed: %v", err) } fmt.Printf("Host: %s\n", host) fmt.Printf("Port: %d\n", port) fmt.Printf("Debug: %v\n", debug) fmt.Printf("Timeout: %d\n", timeout) fmt.Printf("Tags: %v\n", tags) // Reading from file: // cfg, err := jsonconfig.ReadFile("config.json") } ``` -------------------------------- ### Utilize RFC 3339 Time and Closers with types Source: https://context7.com/go4org/go4/llms.txt The types package offers utilities for handling RFC 3339 formatted times, making them JSON-friendly, and provides various closer implementations. This includes `Time3339` for marshaling/unmarshaling, `OnceCloser` to ensure a closer is called only once, `CloseFunc` to wrap functions as closers, and `NopCloser` for no-operation closers. ```go package main import ( "encoding/json" "fmt" "io" "time" "go4.org/types" ) func main() { // Time3339 - JSON-friendly RFC 3339 time type Event struct { Name string `json:"name"` Timestamp types.Time3339 `json:"timestamp"` } // Marshal time to RFC 3339 JSON event := Event{ Name: "deployment", Timestamp: types.Time3339(time.Now()), } data, _ := json.Marshal(event) fmt.Printf("JSON: %s\n", data) // Output: {"name":"deployment","timestamp":"2024-01-15T10:30:00.000000000Z"} // Unmarshal from JSON jsonStr := `{"name":"test","timestamp":"2024-01-15T12:00:00Z"}` var decoded Event json.Unmarshal([]byte(jsonStr), &decoded) fmt.Printf("Parsed time: %v\n", decoded.Timestamp.Time()) // Parse time or get zero value t := types.ParseTime3339OrZero("2024-06-15T10:30:00Z") fmt.Printf("Parsed: %v\n", t.Time()) fmt.Printf("Is zero: %v\n", t.IsAnyZero()) // OnceCloser - ensures Close() only runs once var resource io.Closer = &myResource{} safeCloser := types.NewOnceCloser(resource) safeCloser.Close() // Closes the resource safeCloser.Close() // No-op, returns nil safeCloser.Close() // No-op, returns nil // CloseFunc - turn a function into io.Closer cleanup := types.CloseFunc(func() error { fmt.Println("Cleanup executed") return nil }) defer cleanup.Close() // NopCloser - io.Closer that does nothing _ = types.NopCloser // EmptyBody - ReadCloser that returns EOF body := types.EmptyBody buf := make([]byte, 10) n, _ := body.Read(buf) fmt.Printf("Read %d bytes from EmptyBody\n", n) // 0 } type myResource struct{} func (r *myResource) Close() error { fmt.Println("Resource closed") return nil } ``` -------------------------------- ### Highlight Parse Errors with errorutil in Go Source: https://context7.com/go4org/go4/llms.txt The errorutil package offers utilities for generating more informative error messages, especially for parsing errors. The `HighlightBytePosition` function takes an `io.Reader` and a byte offset, returning the line number, column number, and a highlighted string showing the context around the error. ```go package main import ( "encoding/json" "fmt" "strings" "go4.org/errorutil" ) func main() { // JSON with syntax error badJSON := `{ "name": "test", "value": 123 "missing": "comma" }` var data map[string]interface{} err := json.Unmarshal([]byte(badJSON), &data) if syntaxErr, ok := err.(*json.SyntaxError); ok { line, col, highlight := errorutil.HighlightBytePosition( strings.NewReader(badJSON), syntaxErr.Offset, ) fmt.Printf("JSON syntax error at line %d, column %d:\n", line, col) fmt.Print(highlight) // Output: // 3: "value": 123 // 4: "missing": "comma" // ^ } } ``` -------------------------------- ### Use Panic Helpers for Unrecoverable Errors in Go Source: https://context7.com/go4org/go4/llms.txt The must package provides helper functions that panic when an error occurs. This is useful for initialization code where errors are considered unrecoverable. It includes functions like `must.Close` for deferring resource closing and `must.Do` for executing functions that must succeed. ```go package main import ( "os" "go4.org/must" ) func main() { f, err := os.Open("/tmp/test.txt") if err != nil { panic(err) } // Panics if Close() returns an error defer must.Close(f) // Run function and panic on error must.Do(func() error { // Initialization that must succeed return nil }) } ``` -------------------------------- ### Read HEIF Image Metadata with go4/media/heif Source: https://context7.com/go4org/go4/llms.txt This Go code snippet demonstrates how to use the 'go4.org/media/heif' package to read metadata from HEIF/HEIC image files without decoding the image. It extracts dimensions, rotation information, and EXIF data. Dependencies include the standard Go 'fmt', 'log', and 'os' packages. ```go package main import ( "fmt" "log" "os" "go4.org/media/heif" ) func main() { f, err := os.Open("photo.heic") if err != nil { log.Fatal(err) } defer f.Close() // Open HEIF file hf := heif.Open(f) // Get primary image item item, err := hf.PrimaryItem() if err != nil { log.Fatal(err) } // Get dimensions (before rotation correction) width, height, ok := item.SpatialExtents() if ok { fmt.Printf("Raw dimensions: %dx%d\n", width, height) } // Get rotation (90-degree increments counter-clockwise) rotations := item.Rotations() fmt.Printf("Rotations (x90 degrees): %d\n", rotations) // Get visual dimensions (after rotation) vw, vh, ok := item.VisualDimensions() if ok { fmt.Printf("Visual dimensions: %dx%d\n", vw, vh) } // Extract EXIF data (can be parsed with goexif package) exifData, err := hf.EXIF() if err == heif.ErrNoEXIF { fmt.Println("No EXIF data in file") } else if err != nil { log.Printf("EXIF error: %v", err) } else { fmt.Printf("EXIF data: %d bytes\n", len(exifData)) // Parse with: exif.Decode(bytes.NewReader(exifData)) } // Access item by ID // item, err := hf.ItemByID(itemID) } ``` -------------------------------- ### OAuth2 Token Caching and Authorization Code Flow in Go Source: https://context7.com/go4org/go4/llms.txt Demonstrates how to use oauthutil.TokenSource to manage OAuth2 tokens. It caches tokens to a file and includes a callback function to handle the authorization code flow when a token is not available or expired. This is useful for CLI applications requiring user interaction. ```go package main import ( "bufio" "fmt" "os" "strings" "go4.org/oauthutil" "golang.org/x/oauth2" "golang.org/x/oauth2/google" ) func main() { config := &oauth2.Config{ ClientID: "your-client-id", ClientSecret: "your-client-secret", Endpoint: google.Endpoint, RedirectURL: oauthutil.TitleBarRedirectURL, // For CLI apps Scopes: []string{"https://www.googleapis.com/auth/drive.readonly"}, } // TokenSource that caches to file and prompts for auth code if needed tokenSource := oauthutil.TokenSource{ Config: config, CacheFile: "/tmp/oauth-token.json", AuthCode: func() string { // Generate auth URL for user url := config.AuthCodeURL("state", oauth2.AccessTypeOffline) fmt.Printf("Visit this URL:\n%s\n\n", url) fmt.Print("Enter authorization code: ") reader := bufio.NewReader(os.Stdin) code, _ := reader.ReadString('\n') return strings.TrimSpace(code) }, } // Get token (from cache or by prompting) token, err := tokenSource.Token() if err != nil { fmt.Printf("Error getting token: %v\n", err) return } fmt.Printf("Got token, expires: %v\n", token.Expiry) // Create token source from refresh token refreshSource := oauthutil.NewRefreshTokenSource(config, "your-refresh-token") token, err = refreshSource.Token() if err == nil { fmt.Printf("Refreshed token expires: %v\n", token.Expiry) } } ``` -------------------------------- ### Replace Bytes in Slice with bytereplacer Source: https://context7.com/go4org/go4/llms.txt The bytereplacer package provides efficient in-place replacement of byte sequences within a byte slice. It supports multiple replacement pairs and is optimized for single-byte replacements and scenarios where the result fits within the original slice's capacity. ```go package main import ( "fmt" "go4.org/bytereplacer" ) func main() { // Create a replacer with old->new pairs r := bytereplacer.New( "{{name}}", "John", "{{city}}", "New York", "{{country}}", "USA", ) template := []byte("Hello, {{name}} from {{city}}, {{country}}!") result := r.Replace(template) fmt.Printf("Result: %s\n", result) // Output: Hello, John from New York, USA! // Single-byte replacements are highly optimized sanitizer := bytereplacer.New( "<", "<", ">", ">", "&", "&", ) html := []byte("") safe := sanitizer.Replace(html) fmt.Printf("Sanitized: %s\n", safe) // Output: <script>alert('xss')</script> // In-place replacement when result fits in original capacity data := make([]byte, 0, 100) data = append(data, "Replace A with B"...) replacer := bytereplacer.New("A", "B") result = replacer.Replace(data) fmt.Printf("In-place: %s\n", result) } ``` -------------------------------- ### Duplicate Call Suppression with go4.org/syncutil/singleflight Source: https://context7.com/go4org/go4/llms.txt Provides a mechanism to suppress duplicate function calls for a given key, ensuring only one execution is in-flight. It uses a sync.Group to manage concurrent calls and returns the result of the single execution to all callers. ```go package main import ( "fmt" "sync" "time" "go4.org/syncutil/singleflight" ) func main() { var g singleflight.Group var wg sync.WaitGroup // Expensive operation that should only run once per key expensiveFetch := func(key string) (interface{}, error) { fmt.Printf("Fetching data for %s (expensive operation)\n", key) time.Sleep(100 * time.Millisecond) return fmt.Sprintf("data-for-%s", key), nil } // Launch 5 goroutines requesting the same key simultaneously for i := 0; i < 5; i++ { wg.Add(1) go func(id int) { defer wg.Done() // All calls with same key share one execution val, err := g.Do("user:123", func() (interface{}, error) { return expensiveFetch("user:123") }) if err != nil { fmt.Printf("Goroutine %d: error %v\n", id, err) return } fmt.Printf("Goroutine %d: got %v\n", id, val) }(i) } wg.Wait() // Output: "Fetching..." prints only once, all goroutines get same result } ``` -------------------------------- ### Implement Rolling Checksum with rollsum Source: https://context7.com/go4org/go4/llms.txt The rollsum package implements rolling checksums, similar to those used in rsync and bup, for efficient content-defined chunking and data deduplication. It allows for custom split point detection based on the number of matching bits in the checksum. ```go package main import ( "fmt" "io" "strings" "go4.org/rollsum" ) func main() { rs := rollsum.New() data := strings.NewReader("This is some sample data for demonstrating rolling checksums and split points.") var chunks []string var currentChunk []byte buf := make([]byte, 1) for { _, err := data.Read(buf) if err == io.EOF { break } rs.Roll(buf[0]) currentChunk = append(currentChunk, buf[0]) // Check if we hit a split point (13 consecutive trailing bits match) if rs.OnSplit() { chunks = append(chunks, string(currentChunk)) currentChunk = nil fmt.Printf("Split at digest: %08x, bits: %d\n", rs.Digest(), rs.Bits()) } } // Don't forget the last chunk if len(currentChunk) > 0 { chunks = append(chunks, string(currentChunk)) } fmt.Printf("Total chunks: %d\n", len(chunks)) for i, chunk := range chunks { fmt.Printf("Chunk %d (%d bytes): %q\n", i, len(chunk), chunk) } // Custom split threshold (more bits = larger average chunk size) rs2 := rollsum.New() rs2.Roll('x') if rs2.OnSplitWithBits(10) { // Check for 10 matching bits fmt.Println("Custom split point detected") } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.