TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Monkey Patching in Go

82 pointsby Sirupsenabout 10 years ago

7 comments

GauntletWizardabout 10 years ago
This is awful, dangerous, and compiler designers and writers will yell at you. Well done :)<p>I&#x27;ve always wanted a way to monkey-patch otherwise statically and well-bound code, but only for testing. In C, it&#x27;s fairly easy to set up preprocessor rules to do so, though also litters your primary code with test specific stuff. I have mostly focused on how to integrate this into compiler logic; Building some functionality to replace specified symbols in the build phase. Simply replacing the function pointers in the runtime is brilliant, if more than a little hacky and prone to problems.<p>I look forward to playing around with this.
kylequestabout 10 years ago
This is not monkey patching. This is the good old function&#x2F;api &quot;hooking&quot; that&#x27;s pretty common in the compiled language world: <a href="http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Hooking" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Hooking</a><p>Monkey patching is a less invasive version of hooking used in dynamic languages. Bottom line: if you are dealing with assembly it&#x27;s no longer monkey patching :-)
评论 #9291728 未加载
评论 #9292295 未加载
4adabout 10 years ago
This does not work for any non-trivial function because of missing stack map for the GC and stack copier.
mpnordlandabout 10 years ago
So, this is a step towards implementing plugins for go right? It seems to me like this could be used to implement dynamic code loading.
评论 #9292351 未加载
评论 #9293524 未加载
zikabout 10 years ago
This is admirable work in the pursuit of evil.
infogulchabout 10 years ago
Could this be expanded to hot-swap code as you write it?
marvyabout 10 years ago
Is this safe for such short functions? What if the patch takes more space than the original code?
评论 #9292117 未加载