GoLang Examples - Convert String to Int types with examples

This article three ways to Convert String to Integer in Go Language.

GoLang Examples - Convert String to Int types  with examples

Golang cast String int

usually when we want to perform mathematical calculations on strings which contains text. For example, This will be received an input from a user on a form in web applications, If the user enters any value in the text field.

For example, the string contains numbers “134” and will convert this string to int.

The string is a primitive type that contains a group of characters enclosed in open and close braces({ and }). int is a primitive type that contains numeric values. In Go language, There are different integer types - Int8, Int16, Int32, Int is a generic type which can accommodate values based on platform Operating System.
You can check other posts on Convert Int to String.

Golang Convert String to Int

There are 3 ways to convert a string to Integer type in Go language.

The following are the ways

  • strconv Atoi() function
  • strconv ParseInt() function
  • sprintf format example

strconv ParseInt function example

strconv.parseInt() the function takes a string parameter and returned an integer value

You can check my previous post on parseInt function example
Here is the syntax of a parseInt function

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

Parameters to this function
string - actual string to convert to an int value
base - base value from 2 to 36.
bitSize - This parameter reduces the size of parsed int

Return values from this function
successful conversion - default int64 type value and err=nil is returned
Error occurs conversion - int64 value zero is returned an error is returned with NumError type.
This function accepts a string parameter, convert it into a corresponding int type based on a base parameter. By default, it returns Int64 value Example Here is an example for converting String to an int. The string contains numerical values hexadecimal, octal, and any other numbers

func main() {  
 // bitSize argument example usage  
 fmt.Println(" ======================= bitSize argument example ======================= ")  
 v1, err := strconv.ParseInt("5612", 0, 2)  
 fmt.Println(v1, err, reflect.TypeOf(v1))  
 v2, err := strconv.ParseInt("5612", 0, 8)  
 fmt.Println(v2, err, reflect.TypeOf(v2))  
 v3, err := strconv.ParseInt("5612", 0, 16)  
 fmt.Println(v3, err, reflect.TypeOf(v3))  
 // base argument example usage  
 fmt.Println(" ======================= base argument example ======================= ")  
 v4, err := strconv.ParseInt("5612", 0, 16)  
 fmt.Println(v4, err, reflect.TypeOf(v4))  
 v5, err := strconv.ParseInt("5612", 8, 32)  
 fmt.Println(v5, err, reflect.TypeOf(v5))  
 v6, err := strconv.ParseInt("5612", 16, 64)  
 fmt.Println(v6, err, reflect.TypeOf(v6))  
 // String Argument argument example usage  
 fmt.Println(" ======================= String argument example ======================= ")  
 v7, err := strconv.ParseInt("0x123", 0, 64) // hexdecimal Numeric Conversion  
 fmt.Println(v7, err, reflect.TypeOf(v7))  
 v8, err := strconv.ParseInt("0123", 0, 64) // Octal Number Conversion  
 fmt.Println(v8, err, reflect.TypeOf(v8))  
 v9, err := strconv.ParseInt("123", 0, 64) // any other number  
 fmt.Println(v9, err, reflect.TypeOf(v9))  
 // Invalid String argument conversion example usage  
 fmt.Println(" ======================= Invalid String argument conversion example usage ======================= ")  
 v10, err := strconv.ParseInt("abc", 0, 64) // any other number  
 fmt.Println(v9, err, reflect.TypeOf(v10))  

The output of the above program is

======================= bitSize argument example =======================  
1 strconv.ParseInt: parsing "5612": value out of range int64  
127 strconv.ParseInt: parsing "5612": value out of range int64  
5612 <nil> int64  
 ======================= base argument example =======================  
5612 <nil> int64  
2954 <nil> int64  
22034 <nil> int64  
 ======================= String argument example =======================  
291 <nil> int64  
83 <nil> int64  
123 <nil> int64  
 ======================= Invalid String argument conversion example usage =======================  
123 strconv.ParseInt: parsing "abc": invalid syntax int64  

strconv.Atoi function example

strconv.Atoi() function takes a string and returns the integer type value

Here is function signature of strconv.Atoi() function

func Atoi(s string) (int, error) 

Parameters is an It accepts a string as a parameter returns - if string value is converted to int successfully, returned int value and error as nil value, if error occurs, int value is zero, error is nill returned for example “123” is passed to atoi() function, int value -123 is returned of type int and error is nil “asdfadsf” is passed, returned int value-zero, and error returned with runtime - strconv.Atoi: parsing “asdfadsf”: invalid syntax This function is equivalent to ParseInt(s, 10, 0) which converted of type int

package main  
import (  
func convertStringToInt(str string) {  
 strValue, err := strconv.Atoi(str)  
 if err == nil {  
  fmt.Printf("DataType: %T  \n", strValue) // Display DataType  
  fmt.Println("value - ", strValue)        // variable value  
 fmt.Println("error - ", err)  
func main() {  

When the above program is compiled and executed, return output is as follows

DataType: int  
value -  243  
error -    
error -  strconv.Atoi: parsing "asdfadsf": invalid syntax  

fmt.Sscan function example 

fmt package provides sscan() function which scans string argument and store into variables. This function read the string with spaces and assign into consecutive Integer variables.
The below program has three variations

  • convertStringToInt() function read normal string which contains numeric values converted to integer 
  • customConversion() which contain strings in the string separated by hyphen customConversionWithSpaces() which contains numbers separated by spaces in a string
package main  
import (  
func convertStringToInt(str string) {  
 intValue := 0  
 _, err := fmt.Sscan(str, &intValue)  
 if err == nil {  
  fmt.Printf("value=%d, type: %T\n", intValue, intValue)  
func main() {  
 convertStringToInt("123")           // 123 is returned of int type  
 convertStringToInt("1aa23a")        // 1 is returned of int type  
 convertStringToInt("abcc")          // It gives error - expected integer  
 customConversion("number=00178")    // 00178  
 customConversionWithSpaces("1 3 5") // 1 3 5 is output  
//custom format  
func customConversion(str string) {  
 var intValue int  
 if _, err := fmt.Sscanf(str, "number=%5d", &intValue); err == nil {  
  fmt.Printf("value=%d, type: %T\n", intValue, intValue)  
//custom string contains spaces  
func customConversionWithSpaces(str string) {  
 var m, n, o int  
 fmt.Sscan(str, &m, &n, &o)  
 fmt.Println(m, n, o)  

The output of the above program is

value=123, type: int  
value=1, type: int  
expected integer  
value=178, type: int  
1 3 5  

Which one is better in Performance time taken? Performance of time taken, Benchmark was calculated to find out the time taken for the string to int conversion. parseInt() is first in performance, Next is atoi() funtion, least in performance is sscan function

Similar Posts