Sunday, November 25, 2018

GoLang Examples - Convert Int to String type tutorials

In this blog post, We are going to learn how String conversion from Int type in go language with examples.

Golang cast int, String

    In Any programming language, Anything user entered in text fields always considered as String and saved to Database as Integer. you check my other post on Convert String to Int typeIn some cases, we need to convert this Integer to String. For example, the integer contains numbers 167 and will convert this int to string as "167".

    String and int are primitive data types in Go language. Int is general data type to hold numeric values. The string is a group of characters enclosed in double quotes. In Go Language, Compiler will not cast Primitive conversions implicitly like Int to String conversions. The developer needs to write a code handle this.
Thanks to Standard strconv package from Golang. strconv is a go standard inbuilt in a package that provides String conversions to or from other data types like int, float and boolean. You can check my other posts on String to/from Boolean  

Golang Convert  Int type to String

With strconv standard package, It is very easy to convert Int to String.strconv package provides various implementations

There are 3 ways to convert Integer type to a string in Go language.
  • strconv FormatInt function
  • strconv Itoa function
  • fmt sprintf format example

strconv FormatInt function example 

strconv FormatInt() function takes input values - integer value and base number and returns number represented string value. You can check complete post on strconv FormatInt example

Here is the syntax of this
func FormatInt(input int64, base int) string
Parameters to this function
input - actual number to convert to a string type
base - base value from 2 to 36.
Return values from this function
returned a represented numeric string value 
Here is a complete example of FormatInt
package main

import (
 "fmt"
 "strconv"
)

func main() {
 input := int64(58)
 fmt.Printf("type=%T, output=%v\n", input, input)
 s0 := strconv.FormatInt(input, 2)
 fmt.Printf("type=%T, output=%v\n", s0, s0)
 s1 := strconv.FormatInt(input, 10)
 fmt.Printf("type=%T, output=%v\n", s1, s1)
 s2 := strconv.FormatInt(input, 16)
 fmt.Printf("type=%T, output=%v\n", s2, s2)
}

Output is
type=int64, output=58
type=string, output=111010
type=string, output=58
type=string, output=3a

strconv Itoa function example 

strconv.Itoa() function is simple form FormInt equal to FormatInt(int64(i), 10) function. This takes an Integer value and return string type value Here is a syntax of Itoa function
func Itoa(i int) string 
Following is a complete example of Itoa function
package main

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

func main() {
 var input = 20
 output := strconv.Itoa(input)
 fmt.Println(output)
 fmt.Println("Input type: ", reflect.TypeOf(input))
 fmt.Println("Output type", reflect.TypeOf(output))

}
When the above program is compiled and executed, The output is as follows
A:\Golang\work>go run Test.go
20
Input type:  int
Output type string

fmt Sprintf function example 

package fmt Sprintf() function which accepts any value argument and converts to string type and finally returned.
Here is a syntax of Sprintf() function
func Sprintf(format string, input ...interface{}) string 
It accepts two parameters- format string with %v and input - any value
package main

import (
 "fmt"
 "reflect"
)

func main() {
 var input = 20
 output := fmt.Sprintf("%v", input)
 fmt.Println(output)
 fmt.Println("Input type: ", reflect.TypeOf(input))
 fmt.Println("Output type", reflect.TypeOf(output))

}
output is
20
Input type:  int
Output type string

Related article


EmoticonEmoticon