Go Data Types
12.01.2025 ยท dayanch
In Go, data types are categorized into two main groups: basic types and composite types. Because Go is a statically typed language, every variable must have a defined type at the time of declaration, and that type cannot change.
Basic Types
Numeric Types
- Signed integers.
int, int8, int16, int32, int64
- Unsigned integers.
uint, uint8, uint16, uint32, uint64
- Floating-point numbers.
float32, float64
- Complex numbers.
complex64, complex128
Text Types
- A sequence of characters.
string
- byte Alias for typically used for raw data.
uint8
- rune Alias for , represents a Unicode code point.
int32
Boolean Type
- : Can be
bool
ortrue
.false
Composite Types
Arrays
Fixed-size collections of elements of the same type:
var arr [3]int = [3]int{1, 2, 3}
Slices
Dynamic arrays:
var s []int = []int{4, 5, 6}
Maps
Key-value pairs:
m := map[string]int{"a": 1, "b": 2}
Structs
Custom data structures:
type User struct { Name string Age int }
Interfaces
Abstract types that allow different types to be used interchangeably:
var i interface{} = "hello"
Channels
Used for communication between goroutines:
ch := make(chan int)
Understanding Go's type system is essential for writing robust, clear, and efficient code. By mastering these types, you'll be better equipped to write idiomatic Go programs.