package main
import (
"fmt"
"strconv"
)
func main() {
//int to string
/*
In this case both strconv and fmt.Sprintf do the same job but using the strconv package's
Itoa function is the best choice,
because fmt.Sprintf allocate one more object during conversion.
*/
fmt.Println("\nint to string")
myint := 1234567890123
fmt.Println(myint)
fmt.Printf("%T\n", myint)
int2str := strconv.Itoa(myint)
fmt.Println(int2str)
fmt.Printf("%T\n", int2str)
//by sprintf
fmt.Println("\nint to string by sprintf")
int2str = fmt.Sprintf("%d", myint)
fmt.Println(int2str)
fmt.Printf("%T\n", int2str)
//float to string
fmt.Println("\nfloat to string")
myfloat := 123.45678
fmt.Println(myfloat)
fmt.Printf("%T\n", myfloat)
float2string := fmt.Sprintf("%.3f", myfloat)
fmt.Println(float2string)
fmt.Printf("%T\n", float2string)
}
resault
$ go run main.go
int to string
1234567890123
int
1234567890123
string
int to string by sprintf
1234567890123
string
float to string
123.45678
float64
123.457
string