Use pointers to make generic functions work

This commit is contained in:
2024-10-20 12:20:25 +02:00
parent 8a00759c4b
commit c0f5306715
6 changed files with 43 additions and 45 deletions

16
atom.go
View File

@@ -16,22 +16,20 @@ type Countable interface {
*xml.Attr | *Person | *Category | *Link | *ExtensionElement | *Entry
}
func addToSlice[C Countable](slice []C, countable C) {
if slice == nil {
slice = make([]C, 1)
slice[0] = countable
} else {
slice = append(slice, countable)
func addToSlice[C Countable](slice *[]C, countable C) {
if *slice == nil {
*slice = make([]C, 0)
}
*slice = append(*slice, countable)
}
func deleteFromSlice[C Countable](slice []C, id int) error {
length := len(slice)
func deleteFromSlice[C Countable](slice *[]C, id int) error {
length := len(*slice)
if id > length {
return fmt.Errorf("id %v out of range %v", id, length)
}
slice = append(slice[:id], slice[id+1:]...)
*slice = append((*slice)[:id], (*slice)[id+1:]...)
return nil
}