In this tutorial, we will learn about the int16 data type in Go. We will cover the basics of defining and using int16 values, including how to perform arithmetic operations and compare 16-bit integers.
The int16
data type in Go is used to represent 16-bit signed integer values. The range of an int16
is from -32,768 to 32,767.
Int16 variables in Go can be defined using the var
keyword or by type inference with explicit type conversion.
var x int16 = 32000
y := int16(15000)
Arithmetic operations such as addition, subtraction, multiplication, and division can be performed on int16 values.
sum := x + y
difference := x - y
product := x * y
quotient := x / y
Int16 values can be compared using relational operators.
fmt.Println(x > y)
fmt.Println(x < y)
fmt.Println(x == y)
We can define and use int16 variables in Go to represent 16-bit signed integer values.
For example,
x
and assign it a value.y
using type inference with explicit type conversion and assign it a value.package main
import "fmt"
func main() {
var x int16 = 32000
y := int16(15000)
fmt.Println(x)
fmt.Println(y)
}
32000 15000
We can perform arithmetic operations on int16 values in Go.
For example,
x
and y
.package main
import "fmt"
func main() {
x := int16(32000)
y := int16(15000)
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)
}
Sum: 47000 Difference: 17000 Product: 480000000 Quotient: 2
We can compare int16 values in Go using relational operators.
For example,
x
and y
with different values.package main
import "fmt"
func main() {
x := int16(32000)
y := int16(15000)
fmt.Println(x > y) // true
fmt.Println(x < y) // false
fmt.Println(x == y) // false
}
true false false
We can pass int16 values to functions and return int16 values from functions in Go.
For example,
add
that takes two int16 parameters and returns their sum.package main
import "fmt"
func add(a int16, b int16) int16 {
return a + b
}
func main() {
result := add(10000, 15000)
fmt.Println(result)
}
25000