Integer Number Data Types in GoLang

Integer Number:

Integer numbers are normal numeric numbers or digits. There are two different types of integers in the go language:

  1. Signed integer
  2. Unsigned integer

Both unsigned and signed integers have four different sizes available in GoLang.

Signed and unsigned integers are also categorized into four parts. Here a table is given below, in which we will define all categories of the integer data type. In this table, the unsigned integer will be represented by 'uint,' and the signed integer will be represented by 'int.'

  Data Type  Details
int8Signed integer having an 8-bit size in storage.
int16Signed integer having a 16-bit size in storage.
int32Signed integer having a 32-bit size in storage.
int64Signed integer having a 64-bit size in storage.
uint8Unsigned integer having an 8-bit size in storage.
uint16Unsigned integer having a 16-bit size in storage.
uint32Unsigned integer having a 32-bit size in storage.
uint64Unsigned integer having a 64-bit size in storage.
intIn this data type, both signed integers and unsigned integers have the same size. It may be either 32 bit or 64-bit size in storage.
uintIn this data type, both signed integers and unsigned integers have the same size. It may be either 32 bit or 64-bit size in storage.
runeRune is the synonyms of the int32. It also represents Unicode code points in go language.
byteA byte is also a synonym of int8.
uintptrUintptr is an unsigned integer type data. It is used to hold the address of another variable, the same as a pointer. This data type's width is not defined in the go language, but it can hold all bit size of a pointer value.
Range:

Here, the range of both signed and unsigned integer data types are given below:

For signed integer data type:

  1. int8: The range of the int8 data type variable is -128 to 127.
  2. int16: The range of int16 data type variable is -32768 to 32767.
  3. int32: The range of int32 data type variable is -2147483648 to 2147483647.
  4. int64: The range of int64 data type variable is -9223372036854775808 to 9223372036854775807.

For unsigned integer data type:

  1. uint8: The range of the uint8 data type variable is 0 to 255.
  2. uint16: The range of uint8 data type variable is 0 to 65535.
  3. uint32: The range of uint8 data type variable is 0 to 4294967295.
  4. uint64: The range of uint8 data type variable is 0 to 18446744073709551615.
Example:

Here a go program is given below in which we are going to define a signed and unsigned integer variable and display its range:

 package main
 //  program to define the integer data type and its range.
 import "fmt"
 func main() {
             fmt.Println(" \n signed integer \n ")
             // declaring variable by using 16-bit size of signed integer data type.
             var num1 int16 = 32767                  // variable declared.
             fmt.Printf("The assigned value is : %d \n", num1)
             fmt.Println(num1 + 2, num1 - 2)
             // declaring variable by using 8-bit size of signed integer data type.
             var num2 int8 = 127             // variable declared.
             fmt.Printf("The assigned value is : %d \n", num2)
             fmt.Println(num2 + 2, num2 - 2)
             // declaring variable by using 32-bit size of signed integer data type.
             var num3 int32 = 2147483647                   // variable declared.
             fmt.Printf("The assigned value is : %d \n", num3)
             fmt.Println(num3 + 2, num3 - 2)
             // declaring variable by using 64-bit size of signed integer data type.
             var num4 int64 = 9223372036854775807                       // variable declared.
             fmt.Printf("The assigned value is : %d \n", num4)
             fmt.Println(num4 + 2, num4 - 2)
             fmt.Println("\n unsigned integer \n")
             // declaring variable by using 8-bit size of unsigned integer data type.
             var num5 uint8 = 255                      // variable declared.
             fmt.Printf("The assigned value is : %d \n", num5)
             fmt.Println(num5 + 2, num5 - 2)
             // declaring variable by using 16-bit size of unsigned integer data type.
             var num6 uint16 = 65535                // variable declared.
             fmt.Printf("The assigned value is : %d \n", num6)
             fmt.Println(num6 + 2, num6 - 2)
             // declaring variable by using 32-bit size of unsigned integer data type.
             var num7 uint32 = 4294967295                // variable declared.
             fmt.Printf("The assigned value is : %d \n", num7)
             fmt.Println(num7 + 2, num7 - 2)
             // declaring variable by using 64-bit size of unsigned integer data type.
             var num8 uint64 = 18446744073709551615                  // variable declared.
             fmt.Printf("The assigned value is : %d \n", num8)
             fmt.Println(num8 + 2, num8 - 2)
 } 
Output:

Here, the output of the above program is given below:

Integer Number Data Types in GoLang



ADVERTISEMENT
ADVERTISEMENT