Golang Program to convert string type variables into int


In this tutorial, we will learn how to convert string type variables into int in Go programming language.

For this task various types of string conversions are needed, to perform the conversions “strconv” package is imported in the go language program,

Strings can be transformed into integer values using the ParseInt function. It decodes a string in the specified base (0, 2 to 36) and bit size (0, 64), and then it returns the equivalent result.

Convert String Type Variable Into Int

Syntax

func ParseInt(s string, radix/base int, bitSize int) (i int64, err error)

ParseInt() function is used to convert strings to integer type values. This function accepts two arguments one is the string that we wish to convert and other is a integer type value. This value specifies the size of the result. It can be either 32 or 64 bits. This function returns the final result as a 64 bit integer type number and an error which can be printed on the screen if any problem occurs in the conversion process. If we wish to get the result in 32 bit float number then we have to specify the bitsize as 32 this makes the result convertible to 32 bit float number.

Algorithm

  • Step 1 − Import the package fmt, reflect and strconv

  • Step 2 − Start function main().

  • Step 3 − Declare the String ‘string

  • Step 4 − Display the type of variable using reflect.typeof() function.

  • Step 5 − Convert the string to Int using ParseInt() Method.

  • Step 5 − Print the Integer type and the integer value using the fmt.Println() function.

Example

package main // import the required packages to perform the task import ( "fmt" "reflect" "strconv" ) // fmt package allows us to print anything on the screen // reflect package allows us to get the format of a data type // strconv package allows us to use other pre-defined function // call the main function func main() { // initialize the srting string := "tutorialspoint" // print the type of variable fmt.Println("Type of variable Before conversion is :", reflect.TypeOf(string)) // print the value of string fmt.Println("String value is:", string) // use the ParseInt() Function x, _ := strconv.ParseInt(string, 10, 64) // print the type of variable fmt.Println("Type of variable After conversion is :", reflect.TypeOf(x)) // print the value fmt.Println("Integer value is:", x, "\n") // initialize the other srting string1 := "100" // print the type of variable fmt.Println("Type of variable Before conversion is :", reflect.TypeOf(string1)) // print the value of string fmt.Println("String value is:", string1) // use the ParseInt() Function on string y, _ := strconv.ParseInt(string1, 10, 64) // print the type of variable fmt.Println("Type of variable After conversion is :", reflect.TypeOf(y)) // print tehe value fmt.Println("Integer value is: ", y, "\n") }

Output

Type of variable Before conversion is: string 
String value is: tutorialspoint 
Type of variable After conversion is: int64 
Integer value is: 0 

Type of variable Before conversion is: string 
String value is: 100 
Type of variable After conversion is: int64 
Integer value is: 100

Description of the Code

  • First, we Import the package fmt, reflect, strconv, where reflect package is used print the type of the variable and “strconv” package is used to convert the datatype.

  • Then we start the main() function to perform the task and convert the data type of string to Int.

  • We initialize the string to a var “string”.

  • In the next step, we are printing the type of the variable we have just declared.

  • Then we print the actual value that the data type has in it.

  • Then we are calling ParseInt() function from the “strconv” package of go language, and pass the string to the function.

  • Now we have printed the type of variable, to check if the data type is changed from string to Int or not.

  • At last, we have printed the value of the Int that we have just converted from the string data type using fmt.Printl()

  • We have repeated the above steps for different values to understand this function in a better way.

Conclusion

We have successfully compiled and executed the Golang program code to convert string type variables into int.

Updated on: 14-Nov-2022

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements