-5

Is monkey patching possible in Go as in Ruby?

If so, I have a third party library build, which contains one function called encrypt. Now i want to overwrite the behaviour of the function. Is that possible?

lokanadham100
  • 1,149
  • 1
  • 11
  • 22
  • Please give a Ruby example of what you want to achieve in Go. – Grzegorz Żur May 03 '18 at 10:54
  • 2
    This is not possible for obvious reasons. – Volker May 03 '18 at 11:07
  • 2
    Of course you can implement as many or few interface as you like. Just define a type with the appropriate methods. I don't understand the question. – Peter May 03 '18 at 11:54
  • Run time shared library? Like the go plugin package? This only works on Linux, AFAIK, though. I'm guessing the OP wants to add/import some struct at runtime. OP, please include what version of Go you are targeting and OS – Keozon May 03 '18 at 13:10
  • See [this answer](https://stackoverflow.com/a/38732121/13860). But don't ever do that, except in extreme debugging situations. – Jonathan Hall May 03 '18 at 17:13

2 Answers2

3

Golang is a compiled language, you'll have to edit the code, recompile and deploy all over again. "monkey patching" isn't possible as if you're talking about changing the software after it is already deployed like ruby. (which is an interpreted language).

But if your concerns are beyond an hotfix and involve adding new code at runtime from external source, you might want to have a look at go plugins. here's a nice medium post about it.

yosher lutski
  • 306
  • 2
  • 8
1

I'm not big on ruby so to make sure we are on the same page: my understanding is that monkey patching allows you to change the underlying behaviours (methods) attached to a given type at runtime.

In go this would be a big fat no no, probably should be in ruby too for the most part :)

Types are concrete in go, everything has to know what it is dealing with at compile time. It's difficult to propose a solution without a clearer understanding of the end goal, can you provide a more concrete example?

SwiftD
  • 5,769
  • 6
  • 43
  • 67