How to convert String to Integer in Golang?
Golang ? Convert String to Integer
String to Integer in Golang
To convert String to Integer in Golang, use Atoi() function of strconv package. The usage of Atoi() function is
// import statement import "strconv" // convert string to integer number, error = strconv.Atoi("14")
where strconv.Atoi() takes string as argument and converts into an integer. The function also returns an error. error object will be null if there is no error.
Atoi means A(Alphabets or String) to i(integer).
Example 1 ? Convert String to Integer
In this example, we will take a string that has only a number and convert it to integer value.
example.go
package main import "fmt" import "strconv" func main() {
var str = "14" // convert string to integer number, error := strconv.Atoi(str) fmt.Println("error:", error) fmt.Println("number:", number) }
Output
error: number: 14
Example 2 ? Convert String to Integer [Negative Scenario]
In this example, we will take a string that has also non-numeric characters. We will try converting it to a number. We should get an error.
example.go
package main import "fmt" import "strconv" func main() {
var str = "14sup." // convert string to integer number, error := strconv.Atoi(str) fmt.Println("error:", error) fmt.Println("number:", number) }
Output
error: strconv.Atoi: parsing "14sup.": invalid syntax number: 0
We received a "invalid syntax" error while trying to parse a string that contains non-numeric characters.
Golang Golang Tutorial Run Golang Program Golang If Else Golang Switch Golang For Loop Golang Comments Golang Functions Golang Array Golang Slice Golang Struct Golang Class
Golang Range Golang Map Golang Goroutine Golang Channel
Golang String Operations Golang String Length Golang String Concatenation Golang Split String Golang String - Get Index of Substr Golang String to Uppercase Golang String to Lowercase Golang Convert String to Integer
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- golang int to int32
- a tour of go swtch
- golang convert string to lowercase tolower example
- sparql by example the cheat sheet
- parse string to int golang
- golang convert string to uppercase toupper example
- how to convert string to integer in golang
- how to check if string ends with a suffix string in go
- the go programming language part 1
- golang int to str
Related searches
- convert string to integer js
- convert string to integer tableau
- how to convert string to int javascript
- how to convert string to int
- how to convert string to char
- how to convert string to array
- python how to convert string to float
- convert string to integer python
- convert string to integer c
- convert string to integer eclipse
- convert string to integer r
- c how to convert string to int