For functions: grep -P '^func funcName\('
For methods: grep -P '^func [^)]+\) methodName\('
func (s *Recv) foo(fn func(x any) err) func bar(y any) (*Recv, err)
As an exaggerated example. Easy to parse but not always easy to read at a glance.An incremental grep tool with just this one transformation rule gets you a lot more mileage out of grep.
[1] https://github.com/minad/consult/blob/screenshots/consult-li...
EDIT: Better demo https://jumpshare.com/s/zMENBSr2LwwauJVjo1wS
It's not far off from my manually-constructed patterns when I want to make sure I find a function definition (and am willing to tolerate some false positives), but I personally prefer fine-grained control over when it's in use.
For methods: grep -P '^func [^)]+\) methodName\('
Hope that helps.
grep -P '^func [^)]+\) methodName\('
you could say grep 'func [^)]*) methodName('
which is a bit less typinghowever, i have to admit that i sort of ensnared myself in my own noose here by being too clever! i forgot that grep's regexp dialect only supports + if you \ it, and it took me six tries to figure out why i wasn't getting any grep hits. there's a lot to be said for the predictability and consistency of pcre!
grep '\) methodName\(' grep: Unmatched ) or \)
but your main point might be right; the few non-method matches to (pcre) '\)\s*\w+\s*\(' in /usr/share/go-1.19 seem to be uncommon things like this: static void __attribute__ ((constructor)) sigsetup(void) {
void poison() __attribute__ ((weak));
C3 = -(R + I) // ADD(5,6) NEG(-5,-6)grep func | grep functionName
If you don't want OOP in the language, but want people to be able to write thing.function(arg), you just make function(thing, arg) and thing.function(arg) equivalent syntax.
Equivalent means that there is no difference at the AST level between o.f(a) and f(o, a), like there is no difference in C among (a + i), a[i], i[a] and (i + a).
However, a this keyword is way better than making the programmers fraction off a parameter and move it to the other side of the function name.