Skip to main content
Ahmed Hassan·

Ported a Python service to idiomatic Go — not a line-by-line hack

Translates code between any two programming languages with idiomatic patterns, library mappings, and behavioral equivalence verification.

Polyglot Code Translator

You are a polyglot engineer translating code between languages. Produce idiomatic, not literal, translations. ## Source Code ({{source_language}}) ```{{source_language}} {{source_code}} ``` ## Translation Spec - Source: {{source_language}} - Target: {{target_language}} - Target runtime: {{target_runtime}} (e.g., Node 20, Python 3.12, Go 1.22, JVM 21) - Target paradigm: {{target_paradigm}} (keep same, or convert to OOP/functional) ## Translation Rules 1. Use IDIOMATIC {{target_language}} - not a literal line-by-line translation 2. Map standard library calls to {{target_language}} equivalents (provide mapping table) 3. Convert error handling to {{target_language}} conventions (exceptions vs Result vs error returns) 4. Apply {{target_language}} type system fully (generics, type inference, optionals) 5. Convert concurrency model appropriately (threads, async/await, goroutines, etc.) 6. Follow {{target_language}} naming conventions and project structure 7. Use popular {{target_language}} libraries where the source uses external deps 8. Preserve ALL behavior including edge cases and error conditions ## Output 1. **Library Mapping Table** - Source lib → Target lib for each dependency 2. **Translated Code** - Complete, compilable/runnable code 3. **Key Differences** - Behavioral differences between languages to watch for 4. **Setup Instructions** - How to run the translated code (dependencies, commands) 5. **Tests** - Verification that behavior matches original If a direct equivalent doesn't exist, explain the chosen alternative.

Ergebnisse

Translated your Python retry helper to **idiomatic Go** — not a line-by-line port. Go has no decorators, so this becomes a higher-order function. **Library mapping:** | Python | Go | |--------|-----| | `time.sleep()` | `time.Sleep()` | | `functools.wraps` | (n/a — closures) | | `random.uniform` | `rand.Float64()` | **Go version:** ```go package retry import ( "context" "math" "math/rand" "time" ) // Do runs fn up to attempts times with exponential backoff + jitter. func Do(ctx context.Context, attempts int, fn func() error) error { var err error for i := 0; i < attempts; i++ { if err = fn(); err == nil { return nil } backoff := time.Duration(math.Pow(2, float64(i))) * 100 * time.Millisecond jitter := time.Duration(rand.Float64() * float64(backoff)) select { case <-ctx.Done(): return ctx.Err() // honor cancellation — Python version couldn't case <-time.After(backoff + jitter): } } return err } ``` **Key difference:** the Go version threads `context.Context`, so a caller can cancel mid-backoff — something the Python decorator silently ignored. Call it with `retry.Do(ctx, 5, func() error { return callAPI() })`.

Modell: Claude Sonnet 4

16 Likes5 SavesScore: 10

1 Kommentar

Emily Chen·

Saved straight to my snippets. This is cleaner than what I shipped last sprint.