Skip to content

Commit

Permalink
Go: fix recent regression (broken intToStr)
Browse files Browse the repository at this point in the history
This partially reverts commit 7be4a2b.

The above mentioned commit introduced a bug when converting
`strconv.FormatInt(int64(v), 10)` to `strconv.Itoa(int64(v))` (it should
have been `strconv.Itoa(v)`, because https://pkg.go.dev/strconv#Itoa
accepts `int`, not `int64`). Therefore, apparently all tests using
`intToStr` were broken since then.

However, I don't think the change to `strconv.Itoa` was necessary. The
documentation of [`strconv.Itoa`](https://pkg.go.dev/strconv#Itoa)
explains that it is equivalent to `strconv.Itoa(int64(v))`, and it is
apparently also implemented that way:
https://cs.opensource.google/go/go/+/refs/tags/go1.22.5:src/strconv/itoa.go;l=34

`strconv.FormatInt` has the potential advantage that it accepts the
widest possible integer type `int64`, not `int` which might be only 32
bits wide. `strconv.Itoa` seems to be just a convenience alias with a
less general argument type.
  • Loading branch information
generalmimon committed Jul 12, 2024
1 parent 5d7822f commit fb7e862
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo

override def intToStr(value: Ast.expr): TranslatorResult = {
importList.add("strconv")
ResultString(s"strconv.Itoa(int64(${translate(value)}))")
ResultString(s"strconv.FormatInt(int64(${translate(value)}), 10)")
}

override def floatToInt(value: Ast.expr) =
Expand Down

0 comments on commit fb7e862

Please sign in to comment.