Golang int16 type
last modified May 8, 2025
This tutorial explains how to use the int16
built-in type in Go.
We'll cover basic concepts with practical examples of working with 16-bit integers.
The int16 type represents signed 16-bit integers in Go. It can store values from -32768 to 32767. This type is useful when memory efficiency matters.
In Go, int16
is one of several integer types with specific sizes.
It occupies exactly 2 bytes of memory, making it smaller than the default int.
Basic int16 declaration and initialization
The simplest way to use int16
is to declare and initialize variables.
This example shows basic variable declaration with explicit type.
Note: The default integer type in Go is int
, not int16
.
package main import "fmt" func main() { var a int16 = 32767 // Maximum positive value var b int16 = -32768 // Minimum negative value c := int16(1000) // Type conversion from literal fmt.Println("a:", a) fmt.Println("b:", b) fmt.Println("c:", c) }
We declare three int16
variables with different initialization
methods. The example shows the full range of possible values.
Arithmetic operations with int16
int16
supports all standard arithmetic operations. This example
demonstrates addition, subtraction, multiplication, and division.
package main import "fmt" func main() { x := int16(15000) y := int16(8000) 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) }
All operations maintain the int16
type. Be careful with overflow,
as Go doesn't automatically promote types during arithmetic.
Type conversion with int16
Converting between int16
and other numeric types requires explicit
conversion. This example shows safe type conversion practices.
package main import "fmt" func main() { var i16 int16 = 20000 var i32 int32 = 50000 // Convert int16 to int32 (safe) convertedUp := int32(i16) // Convert int32 to int16 (may lose data) convertedDown := int16(i32) fmt.Println("Original int16:", i16) fmt.Println("Converted to int32:", convertedUp) fmt.Println("Converted back to int16:", convertedDown) // Float conversion f := 123.45 floatConverted := int16(f) fmt.Println("Float converted to int16:", floatConverted) }
Conversions to larger types are always safe, but converting down may truncate data. Always check value ranges when converting between types.
Using int16 in arrays and slices
int16
is particularly useful in collections to save memory. This
example demonstrates arrays and slices with int16
elements.
package main import "fmt" func main() { // Array of int16 values arr := [5]int16{10, 20, 30, 40, 50} fmt.Println("Array:", arr) // Slice of int16 values slice := []int16{100, 200, 300, 400} fmt.Println("Original slice:", slice) // Append to slice slice = append(slice, 500, 600) fmt.Println("Appended slice:", slice) // Memory efficient large slice largeSlice := make([]int16, 0, 10000) fmt.Printf("Large slice len: %d, cap: %d\n", len(largeSlice), cap(largeSlice)) }
Using int16
in large collections can significantly reduce memory
usage compared to default int
types.
Edge cases and overflow handling
int16
has limited range, so overflow is a concern. This example
shows how to detect and handle potential overflow situations.
package main import ( "fmt" "math" ) func safeAdd(a, b int16) (int16, bool) { if b > 0 >> a > math.MaxInt16-b { return 0, false // Overflow would occur } if b < 0 >> a < math.MinInt16-b { return 0, false // Underflow would occur } return a + b, true } func main() { max := int16(math.MaxInt16) min := int16(math.MinInt16) // Safe addition result, ok := safeAdd(max-100, 99) fmt.Printf("Safe add: %d, ok: %v\n", result, ok) // Overflow case result, ok = safeAdd(max, 1) fmt.Printf("Overflow add: %d, ok: %v\n", result, ok) // Underflow case result, ok = safeAdd(min, -1) fmt.Printf("Underflow add: %d, ok: %v\n", result, ok) }
The safeAdd
function checks for potential overflow before performing
addition. This pattern can be extended to other operations.
Source
This tutorial covered the int16
type in Go with practical examples
of declaration, arithmetic, conversion, collections, and overflow handling.
Author
List all Golang tutorials.