This is my first post here. I want to,
- extend the
html.Tokenizer
with new methods of my own - while still able to access all existing
html.Tokenizer
methods in the mean time - define a function
WalkBody()
(or an interface method if possible) - in which an interface method of
VisitToken()
is used, which will behave differently for different types
After an extended discussion in [go-nuts]
mlist, I changed the sample code of https://github.com/suntong/lang/blob/master/lang/Go/src/xml/htmlParserTokens.go to https://github.com/suntong/lang/blob/master/lang/Go/src/xml/htmlParserTokens2.go according to all the suggestions there.
However, all suggestions so far just kept to one focus, but not all four altogether. E.g., the first two goals can be achieved by “type MyTokenizer struct”, but as soon as I change that to the interface type to act as the base for the two different extended types (for goal#3&4), goal#2 breaks. I.e.,
this works,
type MyTokenizer struct { *html.Tokenizer } func NewMyTokenizer(i io.Reader) *MyTokenizer { z := html.NewTokenizer(i) return &MyTokenizer{z} }
As soon as I tried the suggested following, everything started to break down.
type TokenVisitor interface { VisitToken() } func WalkBody(of TokenVisitor) { // here you call of.VisitToken() } type MyTokenizer1 struct { *html.Tokenizer } func (the MyTokenizer1) VisitToken() { } type MyTokenizer2 struct { *html.Tokenizer } func (the MyTokenizer2) VisitToken() { }
Seem to me some compromise has to be made, what is the least compromise to make?
The actual purpose for me to ask this question is that, I have a systematic thinking in OO how to solve such kind of inherit & enhance problem, and there is a practical implementation in place for me, the virtual functions. But when it comes to Go, I still need help for how to think, and how to do.
Anybody can help please? Is there any systematic way to deal with the situations like this?