Make:
Purpose: Allocates and initializes memory for specific data structures like slices, maps, and channels.
Return type: The specific data structure type (e.g., []T, map[K]V, chan T).
Initialization: The allocated memory is initialized with default values depending on the data structure type :
- Slices: Empty slice with a nil pointer and capacity of 0.
- Maps: Empty map with a nil pointer.
- Channels: Unbuffered channel that can block operations until data is available or space becomes free.
Usage: Creating new instances of slices, maps, and channels for data storage and communication.
Example:
Purpose: Allocates and initializes memory for specific data structures like slices, maps, and channels.
Return type: The specific data structure type (e.g., []T, map[K]V, chan T).
Initialization: The allocated memory is initialized with default values depending on the data structure type :
- Slices: Empty slice with a nil pointer and capacity of 0.
- Maps: Empty map with a nil pointer.
- Channels: Unbuffered channel that can block operations until data is available or space becomes free.
Usage: Creating new instances of slices, maps, and channels for data storage and communication.
Example:
slice := make([]int, 5) // Creates a slice of integers with a capacity of 5 (can hold up to 5 elements)
myMap := make(map[string]string) // Creates an empty map to store string key-value pairs
ch := make(chan int) // Creates an unbuffered channel for integer communication