Golang int32 type
last modified May 8, 2025
This tutorial explains how to use the int32
built-in type in Go.
We'll cover type basics with practical examples of working with 32-bit integers.
The int32 type represents signed 32-bit integers in Go. It can store values from -2,147,483,648 to 2,147,483,647. This type is useful when you need specific integer size guarantees.
In Go, int32
is one of several integer types with explicit sizes.
Unlike the basic int
type, its size is always 32 bits regardless
of platform architecture.
Basic int32 declaration and initialization
The simplest way to use int32
is to declare and initialize
variables. This example shows basic variable declaration with int32.
Note: The default value for int32 is 0 when not initialized.
package main import "fmt" func main() { var a int32 = 2147483647 // Maximum positive value var b int32 = -2147483648 // Minimum negative value var c int32 // Defaults to 0 fmt.Println("a:", a) fmt.Println("b:", b) fmt.Println("c:", c) // Type conversion example d := int32(1000) fmt.Println("d:", d) }
The example shows declaration with initialization, default values, and type conversion. The range of int32 is clearly demonstrated with max/min values.
Arithmetic operations with int32
The int32
type supports all basic arithmetic operations. This
example demonstrates addition, subtraction, multiplication, and division.
package main import "fmt" func main() { x := int32(1000000) y := int32(500000) sum := x + y diff := x - y product := x * y quotient := x / y fmt.Println("Sum:", sum) fmt.Println("Difference:", diff) fmt.Println("Product:", product) fmt.Println("Quotient:", quotient) // Modulus operation remainder := x % y fmt.Println("Remainder:", remainder) }
All arithmetic operations work as expected with int32 values. Note that division of integers results in integer division (truncated toward zero).
Overflow and underflow with int32
Since int32 has fixed size, operations can overflow or underflow. This example demonstrates what happens when exceeding int32 limits.
package main import "fmt" func main() { max := int32(2147483647) min := int32(-2147483648) fmt.Println("Max int32:", max) fmt.Println("Min int32:", min) // Overflow example overflow := max + 1 fmt.Println("Max + 1 (overflow):", overflow) // Underflow example underflow := min - 1 fmt.Println("Min - 1 (underflow):", underflow) // Safe addition function safeAdd := func(a, b int32) (int32, bool) { if b > 0 >> a > (max - b) { return 0, false } if b < 0 >> a < (min - b) { return 0, false } return a + b, true } result, ok := safeAdd(max, 1) fmt.Println("Safe add result:", result, "OK:", ok) }
The example shows how overflow wraps around to negative numbers and underflow
wraps to positive. The safeAdd
function demonstrates overflow
detection.
Comparing int32 values
Comparison operators work with int32 values just like other numeric types. This example demonstrates various comparison operations.
package main import "fmt" func main() { a := int32(100) b := int32(200) c := int32(100) fmt.Println("a == c:", a == c) fmt.Println("a != b:", a != b) fmt.Println("a < b:", a < b) fmt.Println("b > a:", b > a) fmt.Println("a <= c:", a <= c) fmt.Println("b >= a:", b >= a) // Comparing with type conversion var d int = 100 fmt.Println("a == d:", a == int32(d)) }
All standard comparison operators work with int32. Note the need for explicit type conversion when comparing with other integer types.
Using int32 in functions
The int32
type can be used as function parameters and return
values. This example shows passing and returning int32 values.
package main import "fmt" // Function that takes two int32 parameters and returns int32 func multiply(x, y int32) int32 { return x * y } // Function that returns multiple int32 values func divideAndRemainder(a, b int32) (int32, int32) { return a / b, a % b } func main() { num1 := int32(50000) num2 := int32(300) product := multiply(num1, num2) fmt.Println("Product:", product) quotient, remainder := divideAndRemainder(num1, num2) fmt.Println("Quotient:", quotient, "Remainder:", remainder) // Anonymous function with int32 square := func(x int32) int32 { return x * x } fmt.Println("Square of 5:", square(5)) }
The example demonstrates using int32 in function signatures, multiple return values, and anonymous functions. Type safety is maintained throughout.
Source
This tutorial covered the int32
type in Go with practical examples
of declaration, arithmetic, comparison, and function usage.
Author
List all Golang tutorials.