4 ms·
Monkey Patching in Golang (2015)
- meddlepal 7y agoHmm could be an interesting way to decorate funcs with logging.
- pjmlp 7y agoThis is basically the idea behind Aspect Oriented Programming. https://en.wikipedia.org/wiki/Aspect-oriented_programming https://en.wikipedia.org/wiki/Aspect-oriented_programming
- meddlepal 7y agoYea I was aware... I hesitated to suggest this could lead to AOP for Go because I'm not fond of AOP and it's magicalness at all (used to write a lot of Java)
- rodrigobrito 7y agoRead the license before use the library: https://github.com/bouk/monkey/blob/master/LICENSE.md https://github.com/bouk/monkey/blob/master/LICENSE.md Caution! This approach is very dangerous, and not thread safe.
- giancarlostoro 7y ago> I do not give anyone permissions to use this tool for any purpose. Don't use it. At least "any purpose" should say anything other than personal testing or something. This is silly otherwise. On the other hand at least he's transparent about it.
- mcny 7y ago> This is silly otherwise. There are many things for which we do not need permission though. I am OK with these words.
- saagarjha 7y agoIt should be thread safe once the write completes, no?
- rob74 7y ago"I took the above code and put it in an easy to use library" ... [but] "I do not give anyone permissions to use this tool for any purpose. Don't use it." Sounds strange, until you consider that he only added the "license" 2 years later. I wonder how many questions from various people wanting to use the tool for various purposes he received during this time?
- bouk 7y agoPeople kept asking for a license so they could use it, so I added a license that clarifies that this is not a project that should be seriously used.
- onionisafruit 7y agoI can imagine the type people looking to actually use this. They are using go for whatever reason but complain about it constantly because they want it to be Ruby on Rails.
- bouk 7y agoPeople really want to mock out dependencies in go, instead of architecting their application properly
- onionisafruit 7y agoI’m all over using gomock to test my packages in isolation. I’ll just continue to do that with generated mocks instead of trying to monkeypatch a language that isn’t intended for that. I’m curious about your motivation for writing this. Did it start out as something legitimate and morphed into a joke when you wanted to see how far you can push it? Or were you taking the piss the whole time?
- bouk 7y agoTaking the piss 100%
- bouk 7y agoThe project is meant as one big joke, so yeah don't use it.
- takeda 7y agoLooks like someone made a MIT licensed version of it: https://github.com/undefinedlabs/go-mpatch https://github.com/undefinedlabs/go-mpatch
- laumars 7y agoThis is from 2015 (though I didn't see it that time round so thanks for the resubmission). Previous discussion: https://news.ycombinator.com/item?id=9290917 https://news.ycombinator.com/item?id=9290917
- pjmlp 7y agoCool to see these system level programming tricks being done in Go. This kind of dirty tricks is sometimes used to mock binary libraries for testing. https://www.microsoft.com/en-us/research/project/detours/ https://www.microsoft.com/en-us/research/project/detours/ https://docs.microsoft.com/en-us/visualstudio/test/isolating-code-under-test-with-microsoft-fakes?view=vs-2019 https://docs.microsoft.com/en-us/visualstudio/test/isolating... Yep, questionable, but sometimes that is the only way to match those QA metrics.
- p410n3 7y agoI know for a fact that detours is used by a lot of malware
- callmeal 7y agoHah reminds me of the spirit of fuckitjs (https://github.com/mattdiamond/fuckitjs https://github.com/mattdiamond/fuckitjs).
- seanwilson 7y agoWhat are some good uses of monkey patching? How do you stop it being a maintenance nightmare?
- papreclip 7y agoSuppose you want to unit test some function you've written that makes use of a library that doesn't implement interfaces. You can either write a wrapper interface for the library and inject this wrapper as your dependency, or you can just use the library as-is and hook the function calls in your tests using OP's library. The first method might be the "proper" way of doing things, but you end up with slightly more cluttered code purely to serve the needs of your unit tests. In the second case, any unsafe code is never going into production It's sort of like asking "What's a good use of WriteMemoryProcess or CreateRemoteThread". It's not the kind of tool you acquire and then go find uses for, it's something you will resort to only when you have no other logical choice