Go October 2, 2024 Aditya Rawas

Go Data Types: A Comprehensive Guide for Developers

One of the fundamental aspects of any programming language is its type system. In Go (Golang), data types are designed to be simple and explicit, helping developers write reliable and efficient code. Understanding Go’s type system is essential for building well-optimized applications.

This guide covers every primary data type in Go with examples and explanations.


1. Boolean Type (bool)

The bool type represents truth values: true or false. It’s commonly used in control flow — if statements, loops, and conditions.

var isGoAwesome bool = true

Go does not allow implicit conversions between numeric and boolean types, ensuring stricter type safety than languages like C.


2. String Type (string)

A string in Go is an immutable sequence of bytes encoded in UTF-8. Strings are defined with double quotes.

var greeting string = "Hello, Go!"

Strings can be concatenated with +:

greeting := "Hello, " + "World!"

3. Integer Types

Go provides multiple signed and unsigned integer types with specific bit widths.

Signed Integers

TypeSizeRange
intPlatform-dependent (32 or 64-bit)Varies
int88-bit-128 to 127
int1616-bit-32,768 to 32,767
int3232-bit-2,147,483,648 to 2,147,483,647
int6464-bit-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
var age int = 25
var smallNumber int8 = 100

Unsigned Integers

TypeSizeRange
uintPlatform-dependent0 to max
uint88-bit0 to 255
uint1616-bit0 to 65,535
uint3232-bit0 to 4,294,967,295
uint6464-bit0 to 18,446,744,073,709,551,615
uintptrPlatform-dependentMemory addresses
var itemCount uint = 50
var memoryAddress uintptr

4. Byte and Rune Types

These are type aliases for numeric types, but are semantically meaningful when working with text.

byte (alias for uint8)

Represents a single raw byte of data or an ASCII character.

var letter byte = 'A'

rune (alias for int32)

Represents a Unicode code point, supporting characters from any language.

var unicodeChar rune = ''

5. Floating-Point Types

Go has two floating-point types for decimal numbers:

var pi float64 = 3.14159
var e float32 = 2.71828

When in doubt, use float64 — it provides higher precision and is the default in Go.


6. Complex Number Types

Go includes built-in support for complex numbers:

var c complex64 = complex(1.5, 2.5)
fmt.Println(real(c)) // Output: 1.5
fmt.Println(imag(c)) // Output: 2.5

7. Type Conversions

Go is strongly typed — you cannot mix types without an explicit conversion. This prevents subtle bugs caused by implicit coercion.

Converting int to float64

var integer int = 10
var decimal float64 = float64(integer)

Converting between integer sizes

var small int8 = 127
var large int16 = int16(small)

Always be mindful of range limits when converting to smaller types — Go will not warn you if a value overflows.


8. Type Inference

The := short variable declaration lets Go infer the type automatically from the assigned value:

name := "Golang"    // inferred as string
age := 30           // inferred as int
isDeveloper := true // inferred as bool

Type inference reduces boilerplate without sacrificing type safety — Go’s types remain strict at compile time.


Why Go’s Type System Matters

PropertyBenefit
Type SafetyExplicit conversions prevent implicit coercion bugs
PerformanceSpecific integer sizes (int8, int16, etc.) allow precise memory optimization
Claritybyte and rune make intent clear when handling raw data vs. Unicode
PredictabilityNo surprising implicit casts at runtime

Key Takeaways

Mastering Go’s type system will help you write code that is both correct and performant from day one.