know-how.dev

How to convert int32 into string in Go

1 year ago 602

There are several ways of converting int32 into string in golang.

  1. Using fmt.Sprint(i) (slowest)
import "fmt"

func main() {
	var i int32
	var s string
	i = 10
	s = fmt.Sprint(i)
	fmt.Println(s)
}

// 10
  1. Using strconv.FormatInt(int64(i), 10) (faster)
import (
	"fmt"
	"strconv"
)

func main() {
	var i int32
	var s string
	i = 10
	s = strconv.FormatInt(int64(i), 10)
	fmt.Println(s)
}

// 10
  1. Using strconv.Itoa(int(i)) (fastest)
import (
	"fmt"
	"strconv"
)

func main() {
	var i int32
	var s string
	i = 10
	s = strconv.Itoa(int(i))
	fmt.Println(s)
}

// 10