How do I partition a slice of pointers in Go?

This article explains how to partition a slice of pointers in Go. You'll see an example that demonstrates how to group elements based on a specified criterion.
Go, Golang, pointers, slices, partition, programming, coding
package main

import (
    "fmt"
)

// partition function partitions a slice of pointers based on a predicate function.
func partition(ptrs []*int, predicate func(*int) bool) (included []*int, excluded []*int) {
    for _, ptr := range ptrs {
        if predicate(ptr) {
            included = append(included, ptr) // Add to included
        } else {
            excluded = append(excluded, ptr) // Add to excluded
        }
    }
    return included, excluded
}

func main() {
    a, b, c, d := 1, 2, 3, 4
    data := []*int{&a, &b, &c, &d}

    // Define a simple predicate function
    isEven := func(num *int) bool {
        return *num%2 == 0
    }

    included, excluded := partition(data, isEven)

    fmt.Println("Included (Even):", included)
    fmt.Println("Excluded (Odd):", excluded)
}

Go Golang pointers slices partition programming coding