-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyptr.go
35 lines (29 loc) · 1.02 KB
/
copyptr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package boxpacker3
// CopyPtr creates a copy of a pointer.
//
// It takes a pointer to a generic type T as an argument and returns a new pointer
// to a copy of the original value. If the original pointer is nil, it returns nil.
func CopyPtr[T any](original *T) *T {
// If the original pointer is nil, return nil.
if original == nil {
return nil
}
// Create a copy of the value pointed to by the original pointer.
copyOfValue := *original
// Return a new pointer to the copied value.
return ©OfValue
}
// CopySlicePtr creates a copy of a slice of pointers.
//
// It takes a slice of pointers as an argument and returns a new slice with the same
// elements, but with each element being a copy of the original.
func CopySlicePtr[T any](data []*T) []*T {
// Create a new slice with the same length as the original.
result := make([]*T, len(data))
// Iterate over the original slice and copy each element to the new slice.
for i, item := range data {
result[i] = CopyPtr(item)
}
// Return the new slice.
return result
}