slices
7 articles
-
Represent and transform data safely
Remove Duplicate Values from a Go Slice Without Losing First-Seen Order
Use a map-backed pass to keep the first occurrence of each comparable value, an equality function for other values, or sorting and compacting when order can change.
-
Represent and transform data safely
Find an Element in a Go Slice with `slices.Index` or a Loop
Use `slices.Index` for the first matching position, `slices.Contains` for membership, and a loop when a predicate defines the match.
-
Represent and transform data safely
Sum Values in a Go Slice with a Loop or `slices.Reduce`
Use a range loop for a straightforward `[]int` total, or a third-party reducer when the callback makes a more varied calculation easier to express.
-
Represent and transform data safely
Find the Maximum in a Go Slice Without Sorting It
Find the largest value in a Go slice with a one-pass loop or slices.Max, while making empty-input behavior explicit.
-
Represent and transform data safely
Sort a Go Slice Descending with sort.Slice or sort.Interface
Use sort.Slice for a one-off descending comparison, or define sort.Interface when the ordering is worth reusing.
-
Represent and transform data safely
Go Slices: What append, copy, and cap Actually Do to the Underlying Array
Learn how Go slice length, capacity, aliasing, append, and copy determine whether operations reuse or replace the underlying array.
-
Represent and transform data safely
Go for Loops: Range Semantics and Loop Variable Capture Before and After Go 1.22
Understand how Go 1.22 module boundaries change captured loop variables, how older code behaves, and how to make captures safe in earlier modules.







