int32 in Go
In this tutorial, we will learn about the int32 data type in Go. We will cover the basics of defining and using int32 values, including how to perform arithmetic operations and compare 32-bit integers.
Understanding the Int32 Data Type in Go
The int32
data type in Go is used to represent 32-bit signed integer values. The range of an int32
is from -2,147,483,648 to 2,147,483,647.
Defining an Int32 Variable
Int32 variables in Go can be defined using the var
keyword or by type inference with explicit type conversion.
var x int32 = 2147483647
y := int32(100000)
Performing Arithmetic Operations
Arithmetic operations such as addition, subtraction, multiplication, and division can be performed on int32 values.
sum := x + y
difference := x - y
product := x * y
quotient := x / y
Comparing Int32 Values
Int32 values can be compared using relational operators.
fmt.Println(x > y)
fmt.Println(x < y)
fmt.Println(x == y)
Example 1: Defining and using Int32 Variables
We can define and use int32 variables in Go to represent 32-bit signed integer values.
For example,
- Define an int32 variable named
x
and assign it a value. - Define another int32 variable named
y
using type inference with explicit type conversion and assign it a value. - Print the values of both int32 variables to the console.
Go Program
package main
import "fmt"
func main() {
var x int32 = 2147483647
y := int32(100000)
fmt.Println(x)
fmt.Println(y)
}
Output
2147483647 100000
Example 2: Performing Arithmetic Operations
We can perform arithmetic operations on int32 values in Go.
For example,
- Define two int32 variables named
x
andy
. - Perform addition, subtraction, multiplication, and division on these int32 values.
- Print the results of these operations to the console.
Go Program
package main
import "fmt"
func main() {
x := int32(2147483647)
y := int32(100000)
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: -2147383649 Difference: 2147383647 Product: -1448873564 Quotient: 21474
Example 3: Comparing Int32 Values
We can compare int32 values in Go using relational operators.
For example,
- Define two int32 variables named
x
andy
with different values. - Use relational operators to compare the int32 values and print the results to the console.
Go Program
package main
import "fmt"
func main() {
x := int32(2147483647)
y := int32(100000)
fmt.Println(x > y) // true
fmt.Println(x < y) // false
fmt.Println(x == y) // false
}
Output
true false false
Example 4: Using Int32 in a Function
We can pass int32 values to functions and return int32 values from functions in Go.
For example,
- Define a function named
multiply
that takes two int32 parameters and returns their product. - Call the function with int32 arguments and print the result to the console.
Go Program
package main
import "fmt"
func multiply(a int32, b int32) int32 {
return a * b
}
func main() {
result := multiply(30000, 70000)
fmt.Println(result)
}
Output
2100000000