Convert code from one language to another
FreeA fill-in prompt that translates code from one programming language to another — producing idiomatic, behavior-preserving output plus dependency mapping and flagged caveats, not a literal line-by-line transliteration.
A production-ready port of your code in the target language, written the way a native developer would write it, with the risky or ambiguous mappings flagged for you to review.
This prompt
You are a senior software engineer who has shipped production systems in both {{source_language}} and {{target_language}} and specializes in porting code between them.
Your task: translate the {{source_language}} code below into {{target_language}} that a native {{target_language}} developer would approve in code review — behaviorally identical to the original, idiomatic in the target language, and ready to run without further edits.
Source code to translate ({{source_language}}), between the fences:
===SOURCE CODE START===
{{source_code}}
===SOURCE CODE END===
Target environment and constraints (version, framework, allowed libraries, style):
{{target_context}}
Work through it in this order:
1. Read the source and restate in one or two sentences what it does — its inputs, outputs, side effects, and the edge cases it handles — before writing any target code.
2. Map constructs to idioms, not line by line: data structures, iteration and comprehensions, error and null/optional handling, concurrency, and naming/formatting conventions of {{target_language}}.
3. Map dependencies: replace each source library or built-in API with the closest {{target_language}} equivalent, preferring the standard library over third-party packages.
4. Handle the type system: for a statically typed target, infer and declare precise types; for a dynamically typed target, keep it clear and correctly typed at runtime.
5. Preserve behavior on edge cases: empty inputs, integer overflow, floating-point precision, string encoding, ordering, and off-by-one boundaries.
6. Carry over comments that explain intent; drop noise and dead scaffolding.
7. When a construct has no clean equivalent in {{target_language}}, translate the closest faithful version and record the gap — do not invent an API or emit code you cannot stand behind.
Return your answer in exactly these four sections:
1. Translated code — one complete, runnable {{target_language}} code block.
2. Dependencies — the target packages required and the command to install them (state "standard library only" if none).
3. Translation notes — bullets on the non-obvious mappings and the idiom choices you made and why.
4. Caveats and open questions — anything that could not be translated one-to-one, behavioral differences to verify, and decisions that need a human. Write "None" if there are none.
Constraints:
- Preserve the original's observable behavior. Do not add features, optimizations, or refactors beyond what the port requires.
- Write idiomatic {{target_language}}: follow its naming, formatting, and error-handling conventions rather than transliterating {{source_language}} patterns.
- Translate only the code provided. If the snippet calls external symbols that are not defined here, state your assumption about their behavior in the notes instead of inventing it.
- If any part is genuinely ambiguous or has no equivalent, say so in Caveats rather than guessing.14 fields still empty — the {{like_this}} slots fill in as you type.
See it in action
What this prompt produces with example details filled in:
1. Translated code
```go
package main
import (
"sort"
"strings"
)
// WordCount pairs a word with its frequency.
type WordCount struct {
Word string
Count int
}
// TopWords returns the n most frequent words in text, ordered by
// descending count and then ascending word (alphabetical tiebreak).
func TopWords(text string, n int) []WordCount {
counts := map[string]int{}
for _, word := range strings.Fields(strings.ToLower(text)) {
counts[word]++
}
ranked := make([]WordCount, 0, len(counts))
for word, count := range counts {
ranked = append(ranked, WordCount{Word: word, Count: count})
}
sort.Slice(ranked, func(i, j int) bool {
if ranked[i].Count != ranked[j].Count {
return ranked[i].Count > ranked[j].Count
}
return ranked[i].Word < ranked[j].Word
})
if n > len(ranked) {
n = len(ranked)
}
return ranked[:n]
}
```
2. Dependencies
Standard library only (`sort`, `strings`). No installation needed.
3. Translation notes
- Python returns a list of `(word, count)` tuples. Go has no tuple type, so I introduced a named `WordCount` struct to keep the pair type-safe and readable.
- `text.lower().split()` maps to `strings.Fields(strings.ToLower(text))`. `strings.Fields` splits on any run of whitespace and ignores leading/trailing whitespace — matching Python's argument-less `split()`. `strings.Split` would not, so it was the wrong choice here.
- `counts.get(word, 0) + 1` becomes `counts[word]++`. A Go map returns the zero value (`0`) for a missing `int` key, so the explicit default is unnecessary.
- `sorted(key=lambda kv: (-kv[1], kv[0]))` is reproduced with a two-level comparator: count descending, then word ascending. The explicit word tiebreak makes ordering deterministic without relying on sort stability.
4. Caveats and open questions
- Default parameter: Python's `n=3` default has no Go equivalent (Go lacks default arguments), so callers must pass `n` explicitly. If you need the default, add a `TopWords(text)` wrapper. Left out to avoid assuming your call sites.
- Word boundaries: both versions treat punctuation as part of the word (`"cat,"` differs from `"cat"`). Preserved as-is; add trimming if that was unintended.
- Unicode casing: `strings.ToLower` is Unicode-aware like Python's `str.lower`, but exact casefolding of some scripts can differ. Verify if non-ASCII input matters to you.Tips
- Fill target_context with the exact runtime version, framework, and any library constraints — 'Go 1.22, stdlib only' yields cleaner output than just 'Go'.
- Paste one self-contained function or file at a time; long multi-file inputs dilute accuracy and hide the caveats that matter.
- Always read the 'Caveats and open questions' section before trusting the port — that is where default arguments, precision, and no-equivalent constructs surface.
- For dynamic-to-static ports (e.g. Python to Rust), expect the model to infer types; spot-check those inferred signatures against your real data.
Built by Web Innoventix
Want the work done, not just prompted? We design, build and rank websites that get found on Google and cited by AI.
Get a free quoteMore prompts
Browse all →Build a React component from a description
Turn a plain-English description into a production-ready, accessible React + Tailwind component with a usage example.
Senior code review that catches real bugs
Paste a snippet or PR diff and get a senior-engineer review: real issues ranked by severity, each with the exact input that breaks it and a concrete fix — not a wall of style nitpicks.
Debug an error by root cause, not guesswork
Paste an error and its context to get a traced root-cause diagnosis, a fix that addresses the real cause, and a concrete way to verify the bug is gone.
Write thorough unit tests for a function or component
A fill-in prompt that turns any function or component into a runnable unit test suite covering happy paths, edge cases, boundaries, and error handling in your chosen framework.
Refactor a messy function into clean, maintainable code
A copy-paste ChatGPT prompt that refactors a messy function into clean, maintainable code without changing its behavior — it audits the code, rewrites it, explains each change, and suggests tests to prove nothing broke.
Build a regex from a plain-English description (with test cases)
A fill-in-the-blank ChatGPT prompt that turns a plain-English rule into a working regex for your exact language — with a line-by-line breakdown and a pass/fail table of positive and negative test cases.

