html.NewTokenizer returns a pointer to a Tokenizer. So, you probably want to embed a pointer: type MyTokenizer struct { *html.Tokenizer }
func NewMyTokenizer(i io.Reader) *MyTokenizer { z := html.NewTokenizer(i) return &MyTokenizer{z} } If for some reason your want/need the Tokenizer value, you'll need to dereference it before making it part of your MyTokenizer structure: type MyTokenizer struct { html.Tokenizer } func NewMyTokenizer(i io.Reader) *MyTokenizer { z := html.NewTokenizer(i) return &MyTokenizer{*z} } Jason On Monday, January 1, 2018 at 11:14:14 PM UTC-5, Tong Sun wrote: > > > > On Mon, Jan 1, 2018 at 10:21 PM, Ian Lance Taylor <ia...@golang.org > <javascript:>> wrote: > >> On Mon, Jan 1, 2018 at 6:46 PM, Tong Sun <sunto...@gmail.com >> <javascript:>> wrote: >> > >> > I think I generally understand how embedding >> > (https://golang.org/doc/effective_go.html#embedding) works in GO. >> > However, when it comes to the following problem, I'm at lost again. >> > >> > I'm trying to extend the `html.Tokenizer` with new methods of my own: >> > >> > type MyTokenizer struct { >> > html.Tokenizer >> > } >> > >> > >> > func NewMyTokenizer(i io.Reader) *MyTokenizer { >> > z := html.NewTokenizer(i) >> > return *MyTokenizer(z) >> > return &MyTokenizer{z} >> > } >> > >> > >> > >> > so code like >> > >> > z := html.NewTokenizer(body) >> > ... >> > func parseBody(z *html.Tokenizer) { >> > tt := z.Next() >> > ... >> > testt := z.Token() >> > ... >> > >> > >> > can become: >> > >> > z := NewMyTokenizer(body) >> > ... >> > func (z *MyTokenizer) parseBody() { >> > tt := z.Next() >> > ... >> > >> > testt := z.Token() >> > ... >> > >> > >> > >> > >> > However, I'm really struggling to make it work as I was expected. >> > >> > Somebody help please, what's the proper way to extend a type with new >> > methods of my own, while still able to access all existing methods? >> >> The best way to get help for this is to show us precisely what you >> did, ideally in a small complete, stand-alone, example, > > > The *small complete, stand-alone, example *has been provided in OP as > > https://github.com/suntong/lang/blob/master/lang/Go/src/xml/htmlParserTokens.go > > and tell us >> what you expected to happen, and tell us precisely what happened >> instead. > > > *What expected to happen*: adding the following would work: > > type MyTokenizer struct { > html.Tokenizer > } > > > func NewMyTokenizer(i io.Reader) *MyTokenizer { > z := html.NewTokenizer(i) > return *MyTokenizer(z) > return &MyTokenizer{z} > } > > >> In this case I don't know what to suggest because you didn't >> say what you expect and you didn't say what happened. >> > > Can't make it work. The > > return *MyTokenizer(z) > return &MyTokenizer{z} > > Are just the last two attempts that I make, apart from many other failed > attempts that I've lost track of, but *neither compiles*. > > > Further more, how to extend the above even further? -- >> > >> > - I plan to define an interface with a new method `WalkBody()`, in >> which a >> > "virtual" method of `VisitToken()` is used. >> > - Then I plan to define two different type of MyTokenizer, with their >> own >> > `VisitToken()` methods, so the same `WalkBody()` method defined in >> > MyTokenizer will behave differently for those two different types. >> > >> > How to architect above in Go? >> >> First, think in Go terms, don't think in terms like "virtual method" >> that do not exist in Go. >> >> What you want is something like >> >> type TokenVisitor interface { >> VisitToken() >> } >> >> Then your WalkBody function will take a TokenVisitor, and your >> different types will implement different VisitToken methods. >> >> (I see that you said WalkBody method, but you probably want a WalkBody >> function instead.) >> > > I did meant WalkBody method. See: > > > https://github.com/suntong/lang/blob/master/lang/Go/src/xml/htmlParserTokens.go#L54 > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.