2

I really thought this would be as simple as :

string(myInt)

It appears not.

I am writing a function which takes a slice of ints, and appends each one to a string - and adds a separator between each one. Here is my code.

func(xis *Int16Slice) ConvertToStringWithSeparator(separator string) string{
    var buffer bytes.Buffer
    for i, value := range *xis{
        buffer.WriteString(string(value))
        if i != len(*xis) -1 {
            buffer.WriteString(separator)
        }
    }
    return buffer.String()
}

Please read the sentence below. This is not a duplicate of How to convert an int value to string in Go? - because : I am aware of things like the strconv.Itoa function, but it appears as though that will only work on "regular" ints. It will not support an int16

MickeyThreeSheds
  • 986
  • 4
  • 23
  • 42
  • 1
    Not a duplicate. The final sentence in my question points out that the Itoa function (the solution to that question, which uses standard int types), does not support my data here. – MickeyThreeSheds Aug 22 '18 at 14:50
  • So do an inline conversion: `strconv.Itoa(int(value))` – Adrian Aug 22 '18 at 15:05
  • the question of how to convert an string to an int is absolutelly valid! i guess that the ConvertToStringWithSeparator is not the main topic of the question, is something related to another task that make wide the answer. For example, in my personal opinion i would strike your problem thinking in more little things that can be combined for achieve the ConvertToStringWithSeparator-goal, so first the conversion of scalar value, then apply the conversion to each element of the slice,and then apply a join that concatenates all of the elements in a slice into a string separated by a given char. – Victor Oct 10 '18 at 01:05
  • `strconv.Itoa(int(i))` (int to ASCII) or fmt.Sprintf("%d", i) to set an int to a string. See https://stackoverflow.com/a/62737936/12817546 and https://stackoverflow.com/a/62740786/12817546. `strconv.Atoi(s)` (ASCII to int) to set a string to an int. See https://stackoverflow.com/a/62740786/12817546. –  Jul 09 '20 at 09:19

3 Answers3

7

You can use strconv.Itoa (or strconv.FormatInt if performance is critical) by simply converting the int16 to an int or int64, for example (Go Playground):

x := uint16(123)
strconv.Itoa(int(x))            // => "123"
strconv.FormatInt(int64(x), 10) // => "123"

Note that strconv.FormatInt(...) may be slightly faster according to a simple benchmark:

// itoa_test.go
package main

import (
  "strconv"
  "testing"
)

const x = int16(123)

func Benchmark_Itoa(b *testing.B) {
  for i := 0; i < b.N; i++ {
    strconv.Itoa(int(x))
  }
}

func Benchmark_FormatInt(b *testing.B) {
  for i := 0; i < b.N; i++ {
    strconv.FormatInt(int64(x), 10)
  }
}

Run as $ go test -bench=. ./itoa_test.go:

goos: darwin
goarch: amd64
Benchmark_Itoa-8            50000000            30.3 ns/op
Benchmark_FormatInt-8       50000000            27.8 ns/op
PASS
ok      command-line-arguments  2.976s
maerics
  • 151,642
  • 46
  • 269
  • 291
5

you can use Sprintf:

  num := 33
  str := fmt.Sprintf("%d", num)
  fmt.Println(str)

or Itoa

str := strconv.Itoa(3)
Or Yaacov
  • 3,597
  • 5
  • 25
  • 49
0

You can convert the int16 to int and then use the strconv.Itoa function to convert int16 to string.

Ex: https://play.golang.org/p/pToOjqDKEoi