Go Data Types: A Comprehensive Guide

Understanding data types is fundamental to programming in Go. Data types define the kind of data a variable can hold and the operations that can be performed on it. Go offers a rich set of built-in data types for various programming needs.



Basic Data Types in Go

Go's basic data types are the building blocks for more complex data structures. They include:

Boolean

A boolean data type represents a logical value, either true or false. It's commonly used for conditional statements and logical operations.

Code Snippet
var isComplete bool = true
if isComplete {
    fmt.Println("Task finished!")
}
Output
Task finished!

String

A string is an immutable sequence of characters. Go uses UTF-8 encoding for strings, allowing it to handle text from different languages.

Code Snippet
greeting := "Hello, World!"
fmt.Println(greeting)
Output
Hello, World!

Numeric Types

Numeric data types represent numbers. Go provides several numeric types to handle different ranges and precision requirements.

Integer Types

Signed Integers

Signed integers can store both positive and negative values. The following table lists the signed integer types:

Type Size Range
int Platform dependent -2147483648 to 2147483647 (32-bit), -9223372036854775808 to 9223372036854775807 (64-bit)
int8 8 bits/1 byte -128 to 127
int16 16 bits/2 bytes -32768 to 32767
int32 (rune) 32 bits/4 bytes -2147483648 to 2147483647
int64 64 bits/8 bytes -9223372036854775808 to 9223372036854775807

Unsigned Integers

Unsigned integers can store only positive values. The following table lists the unsigned integer types:

Type Size Range
uint Platform dependent 0 to 4294967295 (32-bit), 0 to 18446744073709551615 (64-bit)
uint8 (byte) 8 bits/1 byte 0 to 255
uint16 16 bits/2 bytes 0 to 65535
uint32 32 bits/4 bytes 0 to 4294967295
uint64 64 bits/8 bytes 0 to 18446744073709551615
uintptr Platform dependent Platform dependent

Note: If no type is specified for numeric values, the default type is int. The uintptr is an unsigned integer type that can hold a pointer address.

Float Types

Float types store positive and negative numbers with decimal points, e.g., 15.5, -20.55, or 4578.5568. The following table lists the floating-point types:

Type Size Range
float32 32 bits/4 bytes -3.4e+38 to 3.4e+38
float64 64 bits/8 bytes -1.7e+308 to +1.7e+308
Code Snippet
age := 30  // int
population := 100000000  // int64
byteValue := byte('A') // uint8
Output
None

Floating-Point Types

float32 and float64 represent real numbers with decimal points. float64 is generally preferred for most numerical calculations due to its higher precision.

Code Snippet
pi := 3.14159  // float64
temperature := 25.5  // float32
Output
None

Complex Types

complex64 and complex128 represent complex numbers in the form a + bi.

Code Snippet
complexNum := complex(3, 4)  // complex128
Output
None

The following table lists the complex numeric types:

Type Size Example
complex64 Consists of float32 as real and imaginary parts 5 + 7i
complex128 Consists of float64 as real and imaginary parts -12.3 + 45.6i

Choosing the right numeric type is crucial for performance and memory efficiency. Consider the range of values your data will hold and the precision required.