The strconv package
is used to convert data types to a string format and also from a string to other basics data types in Go.
Common numeric conversions used are the
The Atoi acronym stands for “ASCII to integer,” while Itoa stands for Integer to ASCII.
The Atoi()
function returns two values:
Here, we can use the below function to convert strings value to an integer, string values are coded with a double string quote wrapped around them. Below is the syntax used for Atoi
.
package main// Syntax for Atoifunc Atoi(str string) (int, err)
Now, let’s use the function to convert a string to an integer value. Here, I am going to demonstrate how to use the strconv
package with the Atoi
function to convert a string.
package mainimport ("fmt""strconv")// Converting a string to integer using the Atoi() functionfunc main() {var strNum = "789"fmt.Printf("Before conversion:\n Type: %T\n Value: %v\n", strNum, strNum)val, err := strconv.Atoi(strNum)if err != nil {return}fmt.Printf("After conversion:\n Type: %T \n Value: %v\n", val, val)}
Go has provided an easy way to convert integer values to string format using the Itoa
function. We can also convert a boolean data type to a string format in Go.
The Itoa()
function returns a string value which is the result of the conversion in string. Here, we can use the below function to convert an integer value to a string, integer values are number values. Below is the syntax used for Itoa
.
package main// Syntax for Itoafunc Itoa(i int) string
Now, let’s use the function to convert an integer to a string format. I am going to demonstrate how to use the strconv
package with the Itoa
function to convert an integer in the example below.
package mainimport ("fmt""strconv")// Converting an integer value to a string formatfunc main() {// Declaring an integervar num = 354fmt.Printf("Before conversion:\n Type: %T \n Value: %v\n", num, num)str := strconv.Itoa(num)fmt.Printf("After conversion:\n Type: %T\n Value: %v\n", str, str)}
We can also convert a boolean data type to a string format in Go.
The FormatBool
function takes in boolean data and returns a string formatted data. The function below is the syntax of the boolean function.
package main// Syntaxfunc FormatBool(b bool) string
Now, let’s convert a boolean value to a string, boolean values are true or false. Below is the syntax used for Itoa
.
package mainimport ("fmt""strconv")// Converting a boolean value to a string formatfunc main() {bl := falsefmt.Printf("Before conversion:\n Type: %T\n Value: %v\n",bl,bl)newBl := strconv.FormatBool(bl)fmt.Printf("After conversion:\n Type: %T\n Value: %v\n",newBl,newBl)fmt.Println("--------------------------------------------------------")blean := truefmt.Printf("Before conversion:\n Type: %T\n Value: %v\n",blean,blean)newBlean := strconv.FormatBool(blean)fmt.Printf("After conversion:\n Type: %T\n Value: %v\n",newBlean,newBlean)}
We have been able to understand how to use and implement the strconv
package in Go.
With the strconv
package, you can easily convert your integer values to strings, a string to integers, boolean to strings, etc.
Have fun playing with the codes.