Create new type based on slice, add custom methods

Hi

I have created a type, Person, and a type Persons (this is a slice of Person). I have then created some methods for the Persons type.

Example:
https://play.golang.com/p/ptZGjhjubVI

The method: AgeMoreThen, does not work. This is because the type has no pointer. But then I create a pointer it will fail.

How should I solve this? I want to make it possible to create new types of other types, with custom methods, sort, etc.

Hi @mickesommar ! You were on the right path, but you’ve forgotten just a bit to fix I think (dereferencing?) :wink: . Solution: The Go Playground

make the receiver a pointer. Your methods should have a pointer receiver func (p *Persons) AgeMoreThen

Alternatively, you can make the two methods be public functions, have no receiver, take in a Persons parameter and return a Persons. In this way you get the advantage of being able to chain your calls: sort(filter(filter(Persons)))

1 Like

Hi there, thanks for the response, it works fine. Merry Christmas.