type MyTokenizer struct {
html.Tokenizer
}
func NewMyTokenizer(i io.Reader) *MyTokenizer {
z := html.NewTokenizer(i)
return *MyTokenizer(z)
return &MyTokenizer{z}
}
z := html.NewTokenizer(body)
...
func parseBody(z *html.Tokenizer) { tt := z.Next()
...
testt := z.Token()
...
z := NewMyTokenizer(body)
...
func (z *MyTokenizer) parseBody() {
tt := z.Next()
...
testt := z.Token()
...
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.)
type MyTokenizer struct {
*html.Tokenizer
}
func NewMyTokenizer(i io.Reader) *MyTokenizer {
z := html.NewTokenizer(i)
return &MyTokenizer{z}
}
type MyTokenizer struct {
html.Tokenizer
}
func NewMyTokenizer(i io.Reader) *MyTokenizer {
z := html.NewTokenizer(i)
return &MyTokenizer{*z}
}
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/FRE_A6cNzW8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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?
For your convenience, you can use this as an easy start, when demonstrating your architectural solution.Thx a lot!
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() {
}
// call WalkBody somewhere with either MyTokenizer1 or MyTokenizer2
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
You want different tokenizer types to be used in the same WalkBody implementation. Interfaces abstract the implementation from the calling computation.I think Ian did answer your question.
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() {
}
// call WalkBody somewhere with either MyTokenizer1 or MyTokenizer2
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
Put your methods on *MyTokenizer.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
1. extend the `html.Tokenizer` with new methods of my own
2. while still able to access all existing html.Tokenizer methods in the mean time
z := MyTokenizer1{html.NewTokenizer(r)}
// don’t take the address, a pointer to a struct of pointers doesn’t make sense unless you are changing the pointers
// z.Next() works here, although maybe you need to do z.Tokenizer.Next()
// z.printElmt(depth, text) works here
3. define a function `WalkBody()` (or an interface method)
4. in which an interface method of `VisitToken()` is used, which will behave differently for different types
Why do you need varying types if you are just using html.Tokenizer methods? What is the difference between each type?
func WalkBody(t *html.Tokenizer, w TokenVisitor) {
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/FRE_A6cNzW8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
Tong, I’m glad this works for you.Dave, reading back I see that we’re giving conflicting advice on pointers for MyTokenizer. While my view is that the dereferences are not necessary here, perhaps you have other reasons to have a pointer in this case?