You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi Mat,
I was brought here by the Medium article so this is very much a learning question for me.
In trying to grok the article and figure out why you used an interface I refactored anno.go without the interface and it seemed to work but I wonder have I missed something.
Listening to "Go Time" you said that you had overused interfaces early on and I have Googled the subject and found opposing advice: 'use interfaces freely' and 'don't overuse interfaces'.
I've only refactored anno.go and run its test so there may be something in the rest of the code that's pertinent to the interface.
So, I'll stop rambling, is there something in this code that is not idiomatic or will come and bite me in the bum or is it just a matter of style:
package main
// This has bare minimum changes to make work without interface.import (
"bytes""fmt""log"
)
// ErrNoMatch is returned when a match was expected but// can not be found.// Can be returned from FieldFunc.typeErrNoMatch []bytefunc (eErrNoMatch) Error() string {
return"no match for '"+string(e) +"'"
}
// Note represents something interesting within// text.typeNotestruct {
Val []byte`json:"val"`Startint`json:"start"`Kindstring`json:"kind"`
}
// Notes is a sortable slice of Note objects.typeNotes []*Note// End calculates the end position of this note.func (n*Note) End() int {
returnn.Start+len(n.Val)
}
// This satisfies sort.Interfacefunc (nNotes) Len() int { returnlen(n) }
func (nNotes) Swap(i, jint) { n[i], n[j] =n[j], n[i] }
func (nNotes) Less(i, jint) bool { returnn[i].Start<n[j].Start }
// FinderFunc represents a function capable of finding// notes.typeFinderFuncfunc(s []byte) (Notes, error)
// FieldFunc returns a FinderFunc that finds notes on a per field basis.// The fn returns true if it's a match, and optionally a subset of the the// match.// The returned []byte must be contained in the source string, otherwise// ErrNoMatch will be returned.funcFieldFunc(kindstring, fnfunc(b []byte) (bool, []byte)) FinderFunc {
returnfunc(src []byte) (Notes, error) {
varposintvarnotesNotesfields:=bytes.Fields(src)
for_, f:=rangefields {
ifok, match:=fn(f); ok {
s:=bytes.Index(src[pos:], match) +posifs==-1 {
// true was returned without the returned bytes// appearing in the match.returnnil, ErrNoMatch(match)
}
notes=append(notes, &Note{
Val: match,
Start: s,
Kind: kind,
})
}
pos+=len(f) +1
}
returnnotes, nil
}
}
// FindManyString runs all finders against the source and returns a// slice of notes or an error.funcFindManyString(srcstring, finders...FinderFunc) (Notes, error) {
returnFindMany([]byte(src), finders...)
}
// FindMany runs all finders against the source and returns a// slice of notes or an error.funcFindMany(src []byte, finders...FinderFunc) (Notes, error) {
varallNotesNotesfor_, finder:=rangefinders {
notes, err:=finder(src)
iferr!=nil {
returnnil, err
}
allNotes=append(allNotes, notes...)
}
returnallNotes, nil
}
funcmain() {
// s := "Find http://www.websites.com/ and # #hashtags and @mentions and @more mentions easily"s:="A simple string with no notes."notes, err:=FindManyString(s, Emails, URLs, Mentions, Hashtags)
iferr!=nil {
log.Fatalln(err)
}
fmt.Println("Source:", s)
for_, note:=rangenotes {
fmt.Printf("Found a %s at [%d:%d]: \"%s\"\n", note.Kind, note.Start, note.End(), note.Val)
}
}
The text was updated successfully, but these errors were encountered:
Hi Mat,
I was brought here by the Medium article so this is very much a learning question for me.
In trying to grok the article and figure out why you used an interface I refactored anno.go without the interface and it seemed to work but I wonder have I missed something.
Listening to "Go Time" you said that you had overused interfaces early on and I have Googled the subject and found opposing advice: 'use interfaces freely' and 'don't overuse interfaces'.
I've only refactored anno.go and run its test so there may be something in the rest of the code that's pertinent to the interface.
So, I'll stop rambling, is there something in this code that is not idiomatic or will come and bite me in the bum or is it just a matter of style:
The text was updated successfully, but these errors were encountered: