How to check if a String is an Integer in Go Language

Hello everyone, in this tutorial, we’ll explore multiple ways to detect if the given String is an int.


Example 1: Using strconv.Atoi() function
package main

import (
"fmt"
"strconv"
)

func main() {
str1 := "5674"
str2 := "123o"
fmt.Println(IsInt(str1))
fmt.Println(IsInt(str2))
}

func IsInt(str string) bool {
_, err := strconv.Atoi(str)
if err != nil {
return false
} else {
return true
}
}
Output:

Author: Joy

More Related Topics,

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

ReactJS - Bootstrap - Buttons

Spring Core | BeanFactoryPostProcessor | Example

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Custom Exception Handling in Quarkus REST API

ReactJS, Spring Boot JWT Authentication Example

Java, Spring Boot Mini Project - Library Management System - Download

Top 5 Java ORM tools - 2024

Java - DES Encryption and Decryption example