| by Arround The Web | No comments

How to Convert String to Integer Type in Golang?

The Go programming language is renowned for its simplicity, concurrency, and efficiency, making it a popular choice among developers. One of the common programming tasks that developers often face is converting a string to an integer type. This can be particularly useful when working with user input, file input/output operations, and other data manipulation tasks.

This tutorial will explore how you can convert the string into an integer type in Golang.

Convert String to Integer Type in Golang

There are two methods for converting a string to an integer type in Golang:

Method 1: Using strconv.Atoi Function

In Golang, there is a built-in function called strconv.Atoi that helps you convert a string to an integer. This function works by taking a string as input and returning the integer at the output. If a string cannot be converted to an integer, it will return an error.

The following is the syntax to use strconv.Atoi in Golang.

i, err := strconv.Atoi(str)

Here, i is an integer variable that stores the converted value from string to integer, err is an error variable that stores errors during the conversion process. The str is the string variable that the function needs to convert to an integer.

The example provided below illustrates the use of strconv.Atoi function in Golang.

package main

 

import (

    "fmt"

    "strconv"

)

 

func main() {

    str := "498"

    num, err := strconv.Atoi(str)

    if err != nil {

        fmt.Println("Error:", err)

    }

    fmt.Printf("Original String: %s, Converted Integer: %d\n", str, num)

}

In the above code, the str variable holds the string value “498” and the strconv.Atoi function is used after that for the conversion of the string to an integer. The num variable holds the converted integer value, while the err variable holds any error that may have occurred during the conversion. In case of a successful conversion, the num variable holds the integer value of the string “498” and then fmt.Printf function prints both the string and integer values at the console.

Output

Method 2: Using strconv.ParseInt Function

There is another function in Golang called strconv.ParseInt can also be used to convert a string to an integer type. The syntax to use this function is given below:

func ParseInt(str string, base int, bitSize int)

Here, the str denotes the string to be converted, base defines the numeric base (from 2 to 36) in which the number is represented in the string. For base 0, the prefix of the string is determined by 0x for hexadecimal, 0 for octal, and 0b for binary. If there is no prefix, it assumes the base to be 10. The bitSize is the bit size of the resulting integer (either 0, 8, 16, 32, or 64).

An example of a string to an integer conversion using the strconv.ParseInt is mentioned below:

package main

 

import (

    "fmt"

    "strconv"

)

 

func main() {

    str := "498"

    i, err := strconv.ParseInt(str, 10, 32)

    if err != nil {

        fmt.Println("Error:", err)

    } else {

        fmt.Println("Converted Integer:", i)

    }

}

The above code uses the string “498” and converts it into an integer using strconv.ParseInt, with a base of 10 and a bit size of 32. The resulting integer is then printed to the console and if an error occurs during the conversion, the error message is printed instead.

Output

Conclusion

Converting a string to an integer is a common task in different programming languages including Golang. The Golang provides two built-in functions called strconv.Atoi and strconv.ParseInt to accomplish this task. The strconv.Atoi function is easy to use and returns an error if it fails to convert the string into an integer. The second function, strconv.ParseInt provides more flexibility by allowing the user to specify the numeric base and bit size. It depends on the user’s choice; of which method they are going to use for converting string to integer type in Golang.

Share Button

Source: linuxhint.com

Leave a Reply