Dynamic package loading like java in golang - go

How Will I import package dynamically and invoke its method in golang like java reflection package, there are solutions of how to invoke method in the same file using golang reflection but what about invoking from different package

What you're describing isn't dynamic package loading, it's just reflection. As long as the package is included in the binary and the types are exported, you can reference it the same way you can reference types in the same package.
Dynamic package loading is a different matter entirely; there's the new plugin support, which is still in its early stages, and not yet supported on all platforms. That's the closest you'll get.
Bear in mind that Go is not Java. Don't try to write Java in Go. It won't work. The platform, language, and standard library are very, very, very different between the two. Java can do dynamic class loading because it has a class loader. All types are dynamically loaded in Java. In Go, nothing is dynamically loaded; the Go compiler produces a native binary, there's nothing dynamic about it.
If you're trying to replicate something you're used to in Java, you're probably best served by rethinking your design entirely in a way that's more suitable to the technology you've chosen (Go), or to choose a technology more suited to the problem you're trying to solve.

Related

Can VPP plugins be implemented using Go?

VPP provides the I/S for developing custom plugins that can be hooked into a graph of nodes. I've only seen examples for such plugins written in the C language, and was wondering whether other language, Go for instance, can also be used to write such plugins.
I have no idea what "VPP" is but nonetheless the answer is: "maybe"; here's why:
Go code is able to interface with C libraries via its facility known as cgo.
cgo is a multiple-faceted thing: it allows you to "export" certain Go functions in a certain way so that they can be called from the C side, and it allows you to call functions from the C side. It also allows you to write bits of inline C code to provide glue for the C side, when necessary.
Since some time Go building toolset (at least its "reference" implementation) provides for compiling Go code into a static or dynamic library with C-compatible API.
See this.
With these things in mind, in theory, it should be possible to do what you're after.
Note some possible obstacles:
Most of the time, if a "platform" allows you to write a "plugin" in C, it presupposes your plugin will make extensive use of the platform's own API.
This usually means your plugin is supposed to include certain header files provided by the platform.
The platform might also require your plugin to link against some platform-provided library (usually shared), or libraries.
cgo can do all of the above, but you will need to scrutinize the API provided by the platform and maybe write Go helpers to make its usage more natural for the Go code.
Building/linking issues (usually the locations of the header files and the libs) may also be a thing to solve.

Is there a standard practice for splitting packages into modular components?

I'm working on a library with multiple layers of functionality. I want developers to be able to import only the parts they need, ie mylib-core, mylib-feature1, mylib-feature2, etc. Each lives in its own git repo. I'd also like to provide a simple mylib package which exposes a default set of functionality which is useful for developers new to the library. See d3.js version 4+ for something very similar to what I'm trying to accomplish.
The problems I've run into are
Apparently you can't share a package name between packages. This is a problem because it would be nice to import all the desired repos and then simply have everything available under the mylib name.
I don't see an obvious way to re-export functionality, in order to build the default mylib package.
Are there good solutions or a more idomatic go way to accomplish what I'm shooting for?
Answering your question, there is no idiomatic way of doing what you want. It is common in JavaScript to import a library and export its members without interference. This is not the case in Golang.
I advise you to host your whole library under a single repo, and split the functionality to packages. The Go compiler will only compile and use the imported packages.
And a tip for the future, Go is very different than almost any other language you previously know 😅

How to use #testable import for frameworks built via Carthage

I've already read answers for this question but I'm not satisfied.
Is there any possibility to use #testable for external libraries e.g. Alamofire, RxRealm (reason why I would like to do that is that, some classes are not open and in some cases it's not possible to create mock in unit tests without overriding real implementation).
Carthage doesn't build frameworks for testing. That's actually a good thing.
Library authors give guarantees on how the public interface of their software behaves, but have the freedom to change the private interface of it as much as necessary.
Instead of trying to subclass the implementation of your third party dependencies types to create test doubles for them, I would recommend to put a protocol in front of the subset of functionality you need from the library and only interact with the protocol in your production code.
If you have a particular case you'd like to test could you please update the question with it? I'd love to see if I can help.

Dart Multiple Translations

If in your application you import a package, which has a translation
and in your application create a translation, you get:
Duplicated library name 'messages_all'. library messages_all;
when using the generate_from_arb functionality.
This applies also to the library in the translated files, e.g. messages_es
The cause is that in generate_localized.dart, the library name(s) are hardcoded.
Is there a good work around ... other than renaming the translation libraries?
... and what exact impact does it have?
Is there a "best practice" for translating multiple libraries?

Software design: how to get DLL version number

I have a DLL used in a compliance scenario (the details of which are irrelevant). The important point is that the main executeable must display the DLL version number. My solution was that the DLL has a function to return it's own version - ie obtain it from the its own version resource and return it as a string.
My reviewer says that the main program should work out the DLL version number. He even gave me some code to get the DLL module handle and extract the version using that.
My question is, which is a better design and why? My feeling is that, using OO principles, I should ask the DLL for its version number. Doing it the other way means that the main program needs to know how the version information is stored and is hence more tightly coupled to the implementation.
Note that I know exactly how to extract the version information from a DLL. My question is about the best place for the code that does this.
Can you clarify the environment that you're working in? For now, since you've already mentioned getting the module handle, I'll assume you're using C++ and calling one of a handful of Win32 functions (GetModuleHandle, LoadLibrary etc).
First of all, I'd be careful about applying OO principles in too wide a context. The object oriented paradigm helps you structure your software in a more maintainable and understandable way, the problem you're describing sounds like it maybe stretches outside of the boundaries of your application. If you want to get information about a separate resource, such as a DLL, you should consider using a standard approach to achieving this to ensure that your code is decoupled from the items that it needs to inspect.
If you introduce a function into the DLL to return the version number to your main application, you have created a tight coupling between your main application and any DLL that needs to supply it's version information (by essentially defining a bespoke API or interface for this).
You should consider using standard, platform-wide functionality to retrieve the information instead This will allow your application to version any DLL for which it can obtain a handle.
Assuming you have an HMODULE for the dll (and you're using C++), call the following functions to get the version...
GetModuleFileNameEx (to get the full path and filename of the DLL if you don't already know this)
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683198(v=vs.85).aspx
using this filename, call
GetFileVersionInfoSize (look these up on MSDN)
This will tell you some crucial information about the file's version metadata (how much info, if any the file has). Assuming this function succeeds, call
GetFileVersionInfo
This will load all the file info metadata into a buffer, then call
VerQueryValue
Supply '\' as the lpSubBlock parameter to get the standard file info metadata (including the version number)
The above functions will allow you to write code to get the version number of any module that your code can get a handle to.
Of course, if you're using C# the solution is much simpler. Hope this helps...

Resources