I want to add a few helper methods attached onto a slice. So I created a type which is of []*MyType Is there any way to add to that slice of MyTypes? append will not recognise the slice.
package main
import "fmt"
type MyType struct{
Name string
Something string
}
type MyTypes []*MyType
func NewMyTypes(myTypes ...*MyType)*MyTypes{
var s MyTypes = myTypes
return &s
}
//example of a method I want to be able to add to a slice
func(m MyTypes) Key() string{
var result string
for _,i := range m{
result += i.Name + ":"
}
return result
}
func main() {
mytype1 ,mytype2 := MyType{Name:"Joe", Something: "Foo"}, MyType{Name:"PeggySue", Something: "Bar"}
myTypes:= NewMyTypes(&mytype1,&mytype2)
//cant use it as a slice sadface
//myTypes = append(myTypes,&MyType{Name:"Random", Something: "asdhf"})
fmt.Println(myTypes.Key())
}
I don't want to wrap it in another type and name the param even though I'm sorta doing it.. Because of json marshalling will probably be different
What would be the way to add to the MyTypes slice?
I really want to just be able to add a method to a slice so it can implement a specific interface and not effect the marshalling.. Is there a different better way?
Thanks