Golang1 min read
Go Data Types
Explore the various data types in Go, including basic types, composite types, and more.
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
int, int8, int16, int32, int64Signed integers.uint, uint8, uint16, uint32, uint64Unsigned integers.float32, float64Floating-point numbers.complex64, complex128Complex numbers.
Text Types
stringA sequence of characters.- byte Alias for
uint8typically used for raw data. - rune Alias for
int32, represents a Unicode code point.
Boolean Type
bool: Can betrueorfalse.
Composite Types
Arrays
Fixed-size collections of elements of the same type:
go
var arr [3]int = [3]int{1, 2, 3}Slices
Dynamic arrays:
go
var s []int = []int{4, 5, 6}Maps
Key-value pairs:
go
m := map[string]int{"a": 1, "b": 2}Structs
Custom data structures:
go
type User struct {
Name string
Age int
}Interfaces
Abstract types that allow different types to be used interchangeably:
go
var i interface{} = "hello"Channels
Used for communication between goroutines:
go
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.