In the Go programming language, variables are containers marked by identifiers
or names, which can hold different values in a program. These variables can hold all different data types, whether they are numbers, words, or any other type. To restrict the type of data stored inside these variables, we need to specify the data type of the variables.
int
is one of the available numeric data types in Go
used to store signed integers. int8
is a version of int
that will only store signed numeric values composed of up to 8 bits; if you, for example, try to store an alphanumeric value in it, or an integer beyond what can be made using 8 bits the program would return an error or result in an integer overflow
.
The numeric data type of int
has several other versions in addition to int8
, which include:
int16
int32
int64
uint8
uint16
uint32
uint64
The data types starting from int
store signed integers while those starting with uint
contain unsigned integers, and the numeric value that follows each data type represents the number of bytes stored.
A variable of type int8
can store integers ranging from - til -1
If an
int8
variable is assigned a value beyond the range mentioned above, then an overflow error occurs, which basically means that theint8
variable cannot properly store the assigned number.
The following code shows the positive limit of the values that an int8
variable can store and what happens if you try storing something larger than the stipulated range of values:
package mainimport "fmt"func main() {// initializing with the maximum allowed positive numbervar num int8 = 127// printing the value and data typefmt.Printf("Value is: %d and type is: %T\n", num, num);// making the value out of range by incrementing by 1num = num+1// printing out new value and typefmt.Printf("Value is: %d and type is: %T\n", num, num);}
As we can see from the outputs of the code above, when we stored the value 127 in num
and printed it, the value came out to be 127. However, just incrementing it by 1 pushed it out of the allowed range, which resulted in the stored value being interpreted as -128 when printed again. When this happens, we call it an overflow
.
In the following example, we declare a variable num
explicitly stating its data type to be int8
. Later, we can use the Printf
function to see that num is indeed stored as an int8
data type:
package mainimport "fmt"func main() {var num int8num = 20fmt.Printf("Data type of %d is %T\n", num, num);}
It is also possible to create const
values of type int8
instead of variables. The only difference is that const
values are just variables whose values can not be changed from what it was initialized to. Then, we check the stored data type again by printing it out using the Printf
function:
const
values must be declared and initialized in the same line.
package mainimport "fmt"func main() {// declaring and initializing a const value with an integerconst c_num int8 = 10// %T represents the type of the variable numfmt.Printf("Data type of %d is %T\n", c_num, c_num);}
Free Resources