On Mon, Jan 1, 2018 at 6:46 PM, Tong Sun <suntong...@gmail.com> 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, and tell us
what you expected to happen, and tell us precisely what happened
instead.  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.



> 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.)

Ian

-- 
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.

Reply via email to