I have a type alias for a slice. And I want to be able to append to the slice (or filter from the slice) when the slice is a pointer receiver:
package main
import (
"fmt"
)
type itself []string
func (h itself) appendToItself(test string) {
h = append(h, test)
}
func main() {
h := itself{"1", "2"}
h.appendToItself("3")
fmt.Println(h, "<- how do I make it [1,2,3]")
}
Log:
[1 2] <- how do I make it [1,2,3]
You need to actually pass a pointer, try:
package main
import (
"fmt"
)
type itself []string
func (h *itself) appendToItself(test string) {
*h = append(*h, test)
}
func main() {
h := itself{"1", "2"}
h.appendToItself("3")
fmt.Println(h, "<- how do I make it [1,2,3]")
}