Sunday, 12 May 2019

Go language: Convert integer to string


Approach 1: str := strconv.Itoa(123)
'strconv.Itoa' method converts an integer to string.

App.go
package main

import (
	"fmt"
	"reflect"
	"strconv"
)

func main() {
	str := strconv.Itoa(123)

	fmt.Println(str, reflect.TypeOf(str))

}
a

Output
123 string

Approach 2: Using fmt.Sprintf("%v",value)


App.go
package main

import (
	"fmt"
	"reflect"
)

func main() {
	str := fmt.Sprintf("%v", 123)

	fmt.Println(str, reflect.TypeOf(str))

}

Output
123 string

Approach 3: Using FormatInt method
str := strconv.FormatInt(int64(123), 10)


App.go
package main

import (
	"fmt"
	"reflect"
	"strconv"
)

func main() {
	str := strconv.FormatInt(int64(123), 10)

	fmt.Println(str, reflect.TypeOf(str))

}

Output
123 string



Previous                                                 Next                                                 Home

No comments:

Post a Comment