package
0.0.0-20241228094237-1747b213bd8e
Repository: https://github.com/bvram/golang.git
Documentation: pkg.go.dev
# README
Key Points to Remember
Slices are reference types, so modifying a slice affects the underlying array.
Slices are more efficient than arrays for dynamic data structures.
Slices can be nil, indicating an empty slice.
You can use make to create slices with initial capacity.
Common Pitfalls and Best Practices
Be Mindful of Capacity: When appending to a slice, ensure it has enough capacity to avoid reallocations.
Avoid Unnecessary Copies: Slicing creates a new slice, but the underlying array remains the same.
Use len and cap: These functions provide information about the slice's length and capacity.
Consider Slices Over Arrays: Slices are more flexible and efficient for most use cases.
Go slices: Performance Considerations
1. Reallocations:
When appending to a slice, if the capacity is not sufficient, a new underlying array is allocated, and the elements are copied over.
To minimize reallocations, you can pre-allocate a slice with a sufficient capacity using the make function:
Consider using a buffer.Buffer from the bytes package for efficient string concatenation.
slice := make([]int, 0, 10) // Initial capacity of 10
2. Slicing Operations:
Slicing creates a new slice header, but it doesn't copy the underlying array elements. This makes slicing a low-cost operation.
However, if you're frequently slicing and modifying slices, it's worth considering the memory implications, especially for large datasets.
3. Looping Over Slices:
The range keyword is the most efficient way to iterate over a slice:
for _, value := range slice {
// Do something with value
}
4. Comparing Slices:
To compare two slices, you'll need to compare each element individually.
The reflect package can be used for more complex comparisons, but it's generally less efficient.
5. Passing Slices to Functions:
Slices are passed by reference, so modifications made to a slice inside a function will be reflected in the caller.