Skip to content

Go से Ruby

Recorder: polyglot Go adapter (हर function के लिए एक generated driver, go build)। Frameworks: net/http, Gin, Echo, Fiber और chi routes को analyze ढूँढ लेता है।

Constructs

  • Errors as values: return 0, fmt.Errorf("bad input %q", s) -> raise ArgumentError, ... (recorder किसी returned non-nil error को raise के रूप में report करता है)। Sentinel errors (var ErrNotFound = errors.New(...)) -> class NotFound < StandardError। panic -> raise (RuntimeError)।
  • Multiple returns -> एक array या छोटा Data.define, जब तक दूसरी value error न हो।
  • Zero values: unset int 0 होता है, string "", nil slice JSON में null बनता है जबकि खाली slice [] बनता है। Recorded JSON से हूबहू मिलाएँ।
  • Structs -> Data.define / Struct; JSON tags (json:"user_id,omitempty") keys तय करते हैं और यह भी कि zero values हटें या नहीं: omitempty को compact/explicit checks से port करें।
  • Maps: Go में iteration order random होता है; recorder sorted या noisy order दिखाता है। जहाँ भी original का output किसी map से आया हो, output से पहले sort करें।
  • Goroutines/channels -> sequential code, या Active Job जब वे background काम थे।
  • Handlers: w.WriteHeader(201); json.NewEncoder(w).Encode(x) -> render json: x, status: :created। Gin c.JSON(200, gin.H{...}) -> render json: {...}। http.Error(w, msg, 400) msg + "\n" को text/plain के रूप में लिखता है: render plain: "#{msg}\n", status: :bad_request।
  • database/sql, sqlx, GORM -> उन्हीं tables पर ActiveRecord।

Traps

  • Integer types: int32/int64/uint8 overflow होकर wrap होते हैं; Ruby में ऐसा कभी नहीं होता। Wrap सिर्फ़ वहीं port करें जहाँ कोई recorded case उस तक पहुँचे। Integer division zero की ओर truncate करता है (-7 / 2 == -3): Ruby floor करता है; .fdiv(b).truncate इस्तेमाल करें। % dividend का sign लेता है: remainder।
  • Rounding: math.Round(-2.5) == -3 (half away from zero), Ruby के round जैसा। math.RoundToEven -> round(half: :even)। fmt.Sprintf("%.2f") -> format("%.2f")।
  • Strings bytes हैं: len(s) bytes गिनता है, s[i] एक byte है, for i, r := range s runes पर चलता है। Ruby का length characters गिनता है: जहाँ original bytes इस्तेमाल करता है वहाँ bytesize / byteslice इस्तेमाल करें।
  • strings.Title, strings.ToUpper Ruby के upcase की तरह Unicode-aware हैं; strings.Fields -> split (बिना argument)।
  • JSON में floats: 1.0 रखने वाले float64 के लिए Go 1 लिखता है; recordings में वह int दिखता है।
  • time.Time nanoseconds के साथ RFC 3339 के रूप में marshal होता है: iso8601(9) और Go की तरह trailing zeros हटाएँ।
इस page को GitHub पर edit करें →