Re:Deadcode: Finding unreachable functions in Go
by erik_seaberg on Tuesday January 23, 2024 @02:27PM
(#39108382)
Last I looked, reflection was missing a lot of things like looking up a function by name.
[ Parent
| on HN ]
Re:Deadcode: Finding unreachable functions in Go
by enneff on Tuesday January 23, 2024 @03:35PM
(#39109377)
Go’s reflect package is not “missing” that feature, it is intentionally omitted as it defeats static analysis. In all my years writing Go (14 now), including some tricky metaprogramming, I have never needed to look up a function by name.
Re:Deadcode: Finding unreachable functions in Go
by erik_seaberg on Tuesday January 23, 2024 @04:07PM
(#39109845)
It creates busywork and ongoing maintenance issues in writing an interpreter. E.g., https://pkg.go.dev/text/template requires every possible function to be manually registered, and that’s not a good use of time.
Re:Deadcode: Finding unreachable functions in Go
by zimpenfish on Tuesday January 23, 2024 @02:50PM
(#39108757)
`MethodByName`[0] allows you to find a method on a struct but I'm assuming you must mean a generic package-level function rather than a method?
Re:Deadcode: Finding unreachable functions in Go
by erik_seaberg on Tuesday January 23, 2024 @03:51PM
(#39109616)
Yeah, you can look up a method but only if you already have the receiver’s type (which you also can’t look up by name, or by listing a package).
Re:Deadcode: Finding unreachable functions in Go
by zimpenfish on Tuesday January 23, 2024 @05:19PM
(#39110761)
You don't need the receiver's type because you're calling `MethodByName` on a `reflect.Value` which you can construct from any receiver.
https://go.dev/play/p/hg-ugl7i0ei demonstrates this, I think?