-1

I am brushing up on golang, and trying to do some basic functionality to get some of the rust off. For some reason when I'm trying to build strings I'm getting random characters added to the end, which I haven't asked for. Example (basic fizzbuzz):

func FizzBuzz(input int) string {
    fizz := "fizz"
    buzz := "buzz"
    var answer strings.Builder

    if input % 3 == 0 {
        answer.WriteString(fizz)
    }
    if input % 5 == 0 {
        answer.WriteString(buzz)
    }
    if input & 3 != 0 && input & 5 != 0 {
        answer.WriteString(string(input))
    }

    return answer.String()
}

This is returning strings like "fizzbuzzɶ" with the added character concatenated to the end.

Any ideas?

D W
  • 21
  • 4

1 Answers1

2

string(int) returns a character with the corresponding unicode code point. You need strconv.Itoa instead.

References:

zerkms
  • 249,484
  • 69
  • 436
  • 539