Senior
List the main data structures.
sobes.tech AI
Answer from AI
The main data structures in Go include:
- Arrays — fixed-size sequences of elements of the same type.
- Slices — dynamic, more convenient wrappers over arrays with the ability to change length.
- Maps — hash tables for storing key-value pairs.
- Structs — user-defined data types that group multiple fields.
- Channels — for communication between goroutines, implementing the CSP pattern.
Example of using a slice and a map:
// Slice of strings
names := []string{"Alice", "Bob", "Charlie"}
// Map for counting occurrences
counts := make(map[string]int)
for _, name := range names {
counts[name]++
}
fmt.Println(counts) // map[Alice:1 Bob:1 Charlie:1]