Use pointers to make generic functions work
This commit is contained in:
16
atom.go
16
atom.go
@@ -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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user