How to Convert string to integer type in Go?
Like most modern languages, Golang includes strings as a built-in type. Let's take an example, you may have a string that contains a numeric value "100". However, because this value is represented as a string, you can't perform any mathematical calculations on it. You need to explicitly convert this string type into an integer type before you can perform any mathematical calculations on it. In order to convert string to integer type in Golang, you can use the following methods.
Atoi() Function
You can use the strconv package's Atoi() function to convert the string into an integer value. Atoi stands for ASCII to integer. The Atoi() function returns two values: the result of the conversion, and the error (if any).
Syntax
func Atoi(s string) (int, error)
Example
package main
import (
"fmt"
"strconv"
"reflect"
)
func main() {
strVar := "100"
intVar, err := strconv.Atoi(strVar)
fmt.Println(intVar, err, reflect.TypeOf(intVar))
}
Output
100 <nil> int
ParseInt() Function
ParseInt interprets a string s in the given base (0, 2 to 36) and bit size (0 to 64) and returns the corresponding value i. This function accepts a string parameter, convert it into a corresponding int type based on a base parameter. By default, it returns Int64 value.Syntax
func ParseInt(s string, base int, bitSize int) (i int64, err error)
Example
package main
import (
"fmt"
"reflect"
"strconv"
)
func main() {
strVar := "100"
intVar, err := strconv.ParseInt(strVar, 0, 8)
fmt.Println(intVar, err, reflect.TypeOf(intVar))
intVar, err = strconv.ParseInt(strVar, 0, 16)
fmt.Println(intVar, err, reflect.TypeOf(intVar))
intVar, err = strconv.ParseInt(strVar, 0, 32)
fmt.Println(intVar, err, reflect.TypeOf(intVar))
intVar, err = strconv.ParseInt(strVar, 0, 64)
fmt.Println(intVar, err, reflect.TypeOf(intVar))
}
Output
100 <nil> int64
100 <nil> int64
100 <nil> int64
100 <nil> int64
Using fmt.Sscan
The 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.
Example
package main
import (
"fmt"
"reflect"
)
func main() {
strVar := "100"
intValue := 0
_, err := fmt.Sscan(strVar, &intValue)
fmt.Println(intValue, err, reflect.TypeOf(intValue))
}
Output
100 <nil> int
Most Helpful This Week
How to use wildcard or a variable in our URL for complex routing?
Regular expression to extract DNS host-name or IP Address from string
How to get struct variable information using reflect package?
Regular Expression to get a string between parentheses in Golang
Find length of Channel, Pointer, Slice, String and Map
Example to compare Println vs Printf
Most Helpful This Week
Convert Float32 to Float64 and Float64 to Float32Regular expression to extract text between square bracketsGet Set and Clear Session in GolangExample: How to use ReadAtLeast from IO Package in Golang?How to get Dimensions of an image type jpg jpeg png or gif ?How to use a mutex to define critical sections of code and fix race conditions?Split URL and Get Parameters from URLRegular expression to extract numbers from a string in GolangWhat is GOPATH?Golang Get current Date and Time in EST, UTC and MST?