go - Adding a method for existing type in Golang: how to rewrite return value's type -
i want extend existing goquery.selection type own method , able use package's selectors. know cannot "patch" existing method -- need create new one. how can force existing package functions use new type? i'm missing in general or there's no "nice" way , it's better use function?
package main import ( "fmt" "github.com/puerkitobio/goquery" ) type customselection goquery.selection func (s *customselection) custommethod() int { return 1 } doc.find("*").each(func(i int, s *goquery.selection) { fmt.println(s.custommethod()) // not works since still "goquery.selection" // how can result customselection type here? })
since inheritance not supported, best practice embed non-local type own local type, , extend it.
in design patterns lingo better known composition: https://en.wikipedia.org/wiki/composition_over_inheritance
Comments
Post a Comment