Golang Program to Check the given Number is an Integer or not

Go ProgrammingProgrammingServer Side Programming

In this tutorial we will discuss how to check if the given number is an integer or not in Go Programming Language.

To check or find the type of variable whether it is an integer or not, we can use %T string format flag, reflect.TypeOf and reflect.ValueOf.Kind functions

Below are three code examples showing the three ways to find if the given number is an integer or not or in other words determine the data type of the variables entered.

Using %T string formatting flag to find the type of number

Syntax

%v  the value in a default format
    when printing structs, the plus flag (%+v) adds field names
%T  a Go-syntax representation of the type of the value

Algorithm

Step 1 − Import the package fmt package

Step 2 − Start function main()

Step 3 − Initialize the variable whose type is to be found

Step 4 − Using the %T string formatting flag to determine the datatype

Step 5 − Print the datatype of the number on the screen using fmt.Printf()

Example

package main // fmt package provides the function to print anything import "fmt" func main() { // Initialize the variables count := 42 number := 77 value := 6.7 //%T’ flag is part of “fmt” package, we can use it along with // fmt.Printf to display the type of variable fmt.Printf("variable count=%v is of type %T \n", count, count) fmt.Printf("variable number=%v is of type %T \n", number, number) fmt.Printf("variable value=%v is of type %T", value, value) // print the result }

Output

variable count=42 is of type int
variable number=77 is of type int
variable value=6.7 is of type float64

Description of code

  • In the above program, we first declare the package main

  • We imported the fmt package that includes the files of package fmt

  • Now start the function main()

  • Next initialize the variables whose data type is to be found with a value

  • Now we use the %T string formatting flag to determine the datatype of the number entered. %T’ flag is part of “fmt” package, we use it along with fmt.Printf to display the type of variable

  • The type of variable is printed on the screen

  • This is the simplest method to check the type of variable in Go language.

Using reflect package’s functions - reflect.TypeOf()

Syntax

func TypeOf(i interface{}) Type

The reflect.TypeOf function returns a value of type reflect.Type, which represents the type of the variable passed into the TypeOf function.

Algorithm

Step 1 − Import the package fmt and reflect package

Step 2 − Start function main()

Step 3 − Initialize the variable whose type is to be found

Step 4 − Calling TypeOf() function to determine the datatype

Step 5 − Print the datatype of the number on the screen using fmt.Println()

Example

package main // import the fmt and reflect package import ( "fmt" "reflect" ) // calling main function func main() { // Initialize the variables num1 := 8 num2 := 6.7 num3 := 10 num4 := 4+7i // using TypeOf() method of reflect package // to determine the datatype of the variables fmt.Println("Using reflect.TypeOf Function") fmt.Println("num1 = ", reflect.TypeOf(num1)) fmt.Println("num2 = ", reflect.TypeOf(num2)) fmt.Println("num3 = ", reflect.TypeOf(num3)) fmt.Println("num4 = ", reflect.TypeOf(num4)) }

Output

Using reflect.TypeOf Function
num1 = int
num2 = float64
num3 = int
num4 = complex128

Description of code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt and we also imported reflect package for reflection of the data type of integer given by the user

  • Now start the function main()

  • Initialize a few variables whose data type you want to reflect on the screen with a value

  • Next calling the TypeOf() function from the reflect package to determine the datatype of the variable

  • Last we print the datatype of the number entered by the user on the screen using fmt.Println()

reflect.ValueOf().Kind() method to find if the given integer is an integer or not

Syntax

func ValueOf (i interface{} .Kind(i interface{}))

The reflect.ValueOf() returns a new value initialized to the passed variable and further by accessing Kind() we can get the variable type.

Algorithm

Step 1 − Import the package fmt and reflect package

Step 2 − Start function main()

Step 3 − Initialize the variable whose type is to be found

Step 4 − Calling ValueOf().Kind() method to determine the datatype

Step 5 − Print the value and kind of the number on the screen using fmt.Printf()

Example

package main // import the fmt and reflect package import ( "fmt" "reflect" ) // calling main function func main() { // Initialize the variables num1 := 5 num2 := 59 num3 := 7.9 num4 := true // using ValueOf().Kind() method of reflect package fmt.Printf("variable num1=%v is of type %v \n", num1, reflect.ValueOf(num1).Kind()) fmt.Printf("variable num2=%v is of type %v \n", num2, reflect.ValueOf(num2).Kind()) fmt.Printf("variable num3=%v is of type %v \n", num3, reflect.ValueOf(num3).Kind()) fmt.Printf("variable num4=%v is of type %v \n", num4, reflect.ValueOf(num4).Kind()) // Print the result }

Output

variable num1=5 is of type int
variable num2=59 is of type int
variable num3=7.9 is of type float64
variable num4=true is of type bool

Description of code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt and we also imported reflect package for reflection of the data type, value of integer given by the user

  • Now start the function main()

  • Initialize a few variables whose value and kind you want to reflect on the screen with a value

  • Next calling the ValueOf().Kind() function from the reflect package to determine the value and kind of the variable

  • Last we print the result on the screen using fmt.Println()

Conclusion

In the above three examples we have successfully compiled and executed the Golang code to find if the number given is an integer or not

raja
Updated on 11-Oct-2022 08:03:20

Advertisements