I have discovered xgo today and it solves a question I have from the begining: why I need a lot of interfaces?
Is anybody using this? Any opinions? Thanks.
I have discovered xgo today and it solves a question I have from the begining: why I need a lot of interfaces?
Is anybody using this? Any opinions? Thanks.
Hello Bain, author of xgo here.
Thanks for discovering xgo. For mocking there are basically two flavors in go:
1.interface
2.monkey patching
For the first one, you always need to make your code testable by wrapping with interface. In my opion, it is an over-engineering for business that does not need multiple implementations.
That's how I felt when I first started writing unit tests for our legacy code base. So the monkey patching way fits my need.
However, prior to xgo there was no gurantee in concurrent safety when monkey patching go, because all back-then libraries use assembly to hack the runtime.
Xgo takes another approach: code rewriting in compiling stage. This way no risk is taken to run tests concurrently.
Xgo has been used in our internal code base widely now. And on github, there is one repo that having merged xgo into it's code base, see here https://github.com/kilianc/base-golang/pull/3. And I have created a few other PRs for different projects to switching from other legacy libraries to xgo.
However, even if you don't want to use monkey patching, xgo has provided another tool called trace to help you understanding your code easily.
See here for more examples: https://github.com/xhd2015/xgo?tab=readme-ov-file#trace
If the logic of your code is simple, you don't really need to use an interface.
But if one day you feel that your code becomes very complex, or difficult to test, then you will know the benefits of interfaces.
I wrote an article about using interfaces to optimize code, you can refer to it.