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

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example