new and make keyword in go
How does new and make keywords allocate memory in golang
new keyword:
newis used to allocate memory for a variable of a specific type, including basic types (like int, float64, etc.) and composite types (like structs)- It returns a pointer to the zero-initialized memory of the specified type
- Use
newwhen you need a pointer to a newly allocated zero value of a type p := new(int)allocates memory on the heap and returns a pointer to it, whereasp := 0simply declares a variable on the stack with an initial value of 0

make keyword:
- The
makefunction is used to create a slice with a specific length and capacity. It gives you more control over the initial size and memory allocation of the slice makeis used to create and initialize slices, maps, and channels, which are types that have underlying data structuresmakereturns the type itself, not a pointer- Use make when you need to initialize and allocate memory for slices, maps, or channels.
- Memory for the underlying array is immediately allocated based on the specified length and capacity.
s := make([]int, 2, 4)allocates memory on the heap, whereass := []int{}will only allocate memory after appending items to it
