Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

go
communitycreator

How to use and implement strconv package in golang

Victor Anyimukwu

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.

Numeric conversions

Common numeric conversions used are the Atoistring to integer method and Itoainteger to string methods.

The Atoi acronym stands for “ASCII to integer,” while Itoa stands for Integer to ASCII.

Atoi function

The Atoi() function returns two values:

  • the result of the conversions in integer
  • the error

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 Atoi
func 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 main

import ( 
  "fmt"
  "strconv"
)

// Converting a string to integer using the Atoi() function
func 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)
}


String conversion

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.

Itoa function

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 Itoa
func 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 main

import (
  "fmt"
  "strconv"
)
// Converting an integer value to a string format
func main() {
  // Declaring an integer
    var  num = 354
		fmt.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.

FormatBool function

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

// Syntax
func 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 main

import (
  "fmt"
  "strconv"
)

// Converting a boolean value to a string format 
func main() {
    bl := false
		fmt.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 := true
		fmt.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.

RELATED TAGS

go
communitycreator
RELATED COURSES

View all Courses

Keep Exploring