Go Programming: Data Types
Go is a statically typed language, meaning that every variable must have a defined type. This helps catch errors at compile time and improves performance. Here's a breakdown of Go's fundamental data types:
1. Basic Types
These are the building blocks for more complex data structures.
Integers: Represent whole numbers.
int: Platform-dependent size (usually 32 or 64 bits). The most common integer type.int8: 8-bit signed integer (-128 to 127)int16: 16-bit signed integer (-32768 to 32767)int32: 32-bit signed integer (-2147483648 to 2147483647)int64: 64-bit signed integer (-9223372036854775808 to 9223372036854775807)uint: Platform-dependent unsigned integer (0 to a large positive number).uint8: 8-bit unsigned integer (0 to 255) - often used for bytes. Alias:byteuint16: 16-bit unsigned integer (0 to 65535)uint32: 32-bit unsigned integer (0 to 4294967295)uint64: 64-bit unsigned integer (0 to a very large positive number)uintptr: An integer type large enough to hold the bit representation of a pointer.
Floating-Point Numbers: Represent numbers with decimal points.
float32: 32-bit floating-point number.float64: 64-bit floating-point number. The most common floating-point type.
Complex Numbers: Represent numbers with real and imaginary parts.
complex64: Complex number withfloat32real and imaginary parts.complex128: Complex number withfloat64real and imaginary parts.
Boolean: Represents truth values.
bool: Can be eithertrueorfalse.
String: Represents a sequence of characters.
string: Immutable sequence of UTF-8 encoded bytes. Strings are enclosed in double quotes.
Rune: Represents a Unicode code point.
rune: Alias forint32. Used to represent individual Unicode characters. Useful for working with text in different languages.
2. Composite Types
These types are built from other types.
Array: A fixed-size sequence of elements of the same type.
var arr [5]int // An array of 5 integers arr[0] = 1 arr[1] = 2Slice: A dynamically-sized sequence of elements of the same type. Slices are more flexible than arrays.
var slice []int // A slice of integers slice = append(slice, 1) slice = append(slice, 2)Map: A collection of key-value pairs. Keys must be comparable types (e.g., integers, strings).
var myMap map[string]int // A map with string keys and integer values myMap = make(map[string]int) myMap["apple"] = 1 myMap["banana"] = 2Struct: A collection of fields. Structs are used to represent complex data structures.
type Person struct { Name string Age int } var person Person person.Name = "Alice" person.Age = 30Pointer: Holds the memory address of another variable.
var x int = 10 var p *int = &x // p points to the memory address of x fmt.Println(*p) // Dereference the pointer to get the value of x (prints 10)
3. Type Conversion
Sometimes you need to convert a value from one type to another. Go provides type conversion syntax.
var i int = 10
var f float64 = float64(i) // Convert int to float64
var s string = string(i) // Convert int to string (rune representation)
Important Considerations:
- Zero Values: When a variable is declared without an initial value, it's assigned a zero value. For example:
int: 0float64: 0.0bool: falsestring: "" (empty string)pointer: nil
- Type Inference: Go can often infer the type of a variable based on the value assigned to it. You can use the
:=operator for short variable declarations with type inference.x := 10 // Go infers that x is an int makevs.new:makeis used to initialize built-in types like slices, maps, and channels. It allocates memory and returns an initialized (but zeroed) value.newallocates memory for a value of a given type and returns a pointer to that memory. The value is zeroed.
This overview provides a solid foundation for understanding Go's data types. Experimenting with these types in code is the best way to solidify your understanding. Refer to the official Go documentation for more detailed information: https://go.dev/doc/basic-types