int8 in Go



In this tutorial, we will learn about the int8 data type in Go. We will cover the basics of defining and using int8 values, including how to perform arithmetic operations and compare 8-bit integers.


Understanding the Int8 Data Type in Go

The int8 data type in Go is used to represent 8-bit signed integer values. The range of an int8 is from -128 to 127.


Defining an Int8 Variable

Int8 variables in Go can be defined using the var keyword or by type inference with explicit type conversion.

var x int8 = 127
y := int8(-128)

Performing Arithmetic Operations

Arithmetic operations such as addition, subtraction, multiplication, and division can be performed on int8 values.

sum := x + y
difference := x - y
product := x * y
quotient := x / y

Comparing Int8 Values

Int8 values can be compared using relational operators.

fmt.Println(x > y)
fmt.Println(x < y)
fmt.Println(x == y)


Example 1: Defining and using Int8 Variables

We can define and use int8 variables in Go to represent 8-bit signed integer values.

For example,

  1. Define an int8 variable named x and assign it a value.
  2. Define another int8 variable named y using type inference with explicit type conversion and assign it a value.
  3. Print the values of both int8 variables to the console.

Go Program

package main
import "fmt"
func main() {
    var x int8 = 127
    y := int8(-128)
    fmt.Println(x)
    fmt.Println(y)
}

Output

127
-128


Example 2: Performing Arithmetic Operations

We can perform arithmetic operations on int8 values in Go.

For example,

  1. Define two int8 variables named x and y.
  2. Perform addition, subtraction, multiplication, and division on these int8 values.
  3. Print the results of these operations to the console.

Go Program

package main
import "fmt"
func main() {
    x := int8(127)
    y := int8(-128)
    sum := x + y
    difference := x - y
    product := x * y
    quotient := x / y
    fmt.Println("Sum:", sum)
    fmt.Println("Difference:", difference)
    fmt.Println("Product:", product)
    fmt.Println("Quotient:", quotient)
}

Output

Sum: -1
Difference: -1
Product: -16256
Quotient: -1


Example 3: Comparing Int8 Values

We can compare int8 values in Go using relational operators.

For example,

  1. Define two int8 variables named x and y with different values.
  2. Use relational operators to compare the int8 values and print the results to the console.

Go Program

package main
import "fmt"
func main() {
    x := int8(127)
    y := int8(-128)
    fmt.Println(x > y)  // true
    fmt.Println(x < y)  // false
    fmt.Println(x == y) // false
}

Output

true
false
false


Example 4: Using Int8 in a Function

We can pass int8 values to functions and return int8 values from functions in Go.

For example,

  1. Define a function named multiply that takes two int8 parameters and returns their product.
  2. Call the function with int8 arguments and print the result to the console.

Go Program

package main
import "fmt"
func multiply(a int8, b int8) int8 {
    return a * b
}
func main() {
    result := multiply(12, 10)
    fmt.Println(result)
}

Output

120