You are viewing v1 of 1. Go to the current version · All versions
Translate code between languages idiomatically
Port code to another language so it reads like native code, with notes on behavior differences and gotchas.
Change note: Initial version
Prompt
Translate the code below from {{from_lang}} to {{to_lang}}.
Purpose of the code and how it is used: {{purpose}}
Target environment (versions, libraries I prefer or must avoid): {{environment}}
Source code:
{{code}}
Requirements:
- Produce idiomatic {{to_lang}}, not a line-by-line transliteration. Use the target language's standard library, naming conventions, error handling style and data structures.
- Preserve behavior exactly, including edge cases. Where languages differ on integer overflow, string encoding, mutability, null handling, sorting stability, floating point formatting, concurrency or iteration order, handle it deliberately.
- Keep the public interface as close as it can reasonably be, and tell me where I must change it.
- Do not add features or remove checks.
Output:
1. The translated code in one block.
2. A table of semantic differences that mattered: construct in the source, what I did in the target, and why.
3. Libraries or dependencies I need to add.
4. Three test cases (inputs and expected outputs) I can run on both versions to confirm they agree.
5. Anything you were unsure about, flagged.Variables
{{from_lang}}FromtextDefault: Python
{{to_lang}}TotextDefault: Go
{{purpose}}Purpose and usagetextDefault: A helper that groups log lines into sessions by user id and a 30 minute inactivity gap
{{environment}}EnvironmenttextDefault: Go 1.22, standard library only
{{code}}Source codelongrequiredPaste the code to translate.
Example output
```go
type Event struct {
UserID string
At time.Time
Line string
}
func GroupSessions(events []Event, gap time.Duration) map[string][][]Event {
sort.SliceStable(events, func(i, j int) bool { return events[i].At.Before(events[j].At) })
out := make(map[string][][]Event)
last := make(map[string]time.Time)
for _, e := range events {
sessions := out[e.UserID]
if prev, ok := last[e.UserID]; !ok || e.At.Sub(prev) > gap {
sessions = append(sessions, nil)
}
sessions[len(sessions)-1] = append(sessions[len(sessions)-1], e)
out[e.UserID] = sessions
last[e.UserID] = e.At
}
return out
}
```
**Differences that mattered**
| Source | Target | Why |
|---|---|---|
| `defaultdict(list)` | map plus explicit append | Go has no default values for missing keys |
| `sorted(events, key=...)` | `sort.SliceStable` | Python's sort is stable; Go's `sort.Slice` is not |
| `datetime` subtraction | `Time.Sub` | Returns `Duration` |
**Dependencies:** none.
**Tests:** empty input returns an empty map; two events 30m01s apart give two sessions; unordered input is sorted first.
**Unsure:** the original mutates the input list order; mine sorts in place too, so callers see the same side effect.Notes
Calling out semantic differences, such as sort stability and default values, is where translations usually go wrong. Running the three cross-check tests on both versions gives real confidence.