Go is a statically-typed language, which means that the type of a variable must be specified when it is declared, and the type of a variable cannot be changed after it has been declared. Go provides several built-in data types, including:
- Numeric types: Go has several numeric data types, including
int
,float32
,float64
, andcomplex64
/complex128
. These types are used to represent numbers in different sizes and with different levels of precision. - Boolean type: The
bool
type is used to represent boolean values (true
orfalse
). - String type: The
string
type is used to represent sequences of characters. Strings in Go are immutable, which means that once a string is created, its value cannot be changed. - Array type: The
array
type is used to represent a fixed-length sequence of elements of the same type. For example:
var a [3]int
In this example, an array a
of length 3 and type int
is declared.
- Slice type: The
slice
type is used to represent a dynamically-sized sequence of elements of the same type. Unlike arrays, slices do not have a fixed length and can grow or shrink as needed. For example:
s := []int{1, 2, 3}
In this example, a slice s
of type int
is declared and initialized with the values 1
, 2
, and 3
.
- Map type: The
map
type is used to represent an unordered collection of key-value pairs. Maps can be used to associate values with keys and are useful for tasks such as counting occurrences or looking up values based on keys. For example:
m := map[string]int{"foo": 42, "bar": 43}
In this example, a map m
of type map[string]int
is declared and initialized with two key-value pairs.
These are the built-in data types in Go. Go also allows users to define their own custom data types, such as structs and interfaces, which can be used to represent complex data structures.