Preface
When it comes to plug-ins, many people are familiar with them. Generally speaking, plug-ins have several advantages, one is to increase program expansion and enrich functions. In addition, hot updates can be implemented. Some large applications often have several GB installation programs. If a small update is used, the entire program needs to be downloaded again. At this time, we can plug in the frequently updated modules, so that only a small update file is needed to be downloaded when updating. For example, our Chrome browser usually installs some plug-ins, which can expand the browser to achieve more functions, and can also flexibly install and uninstall.
Golang provides a Plugin mechanism after version 1.8, which can dynamically load so files and implement plug-in. Although it is not very mature, it is still very easy to use in specific situations.
Currently plugins are only supported on Linux, FreeBSD, and macOS.
1. Start quickly
There is no difference between plug-in code and ordinary code, it is just different when compiling, but the requirement is that there must be only one main package.
package main var Name = "Plugin Name" func GetName() string { return Name }
Compiling with go build -buildmode=plugin will get a so file. How to use this file?
It's very simple, in three steps:
1. Open the so file first. If a plugin has been opened, it will return the existing plugin
2. Use Lookup to find variables or functions that need to be called, the name must start with capitalization.
3. Call after assertion
func main() { //Open the loading plugin, the parameters are the storage location of the plugin, which can be the relative path open, err := ("/home/jwang/Documents/") if err != nil { panic(err) } //Find the identifier lookup, err := ("GetName") if err != nil { panic(err) } res := lookup.(func() string)() ("%v\n", res) name, err := ("Name") if err != nil { panic(err) } ("%v\n", *name.(*string)) }
From the above code, we can see that the use of plug-ins is very simple and easy to understand.
Generally speaking, in order to realize plug-inization, some interfaces can be defined in advance, and then the plug-in implements these interfaces to ensure consistency, but the definition of the interface cannot be written in the plug-in package or the call package. At this time, you need to define a special public package and write the interface definition inside, so that both the plug-in package and the call package can be referenced.
2. Things to note
The reason why this plug-in solution is immature is mainly due to the strong dependence between the main program and the plug-in program, such as:
1. The compiled GO version must be completely consistent
2. The public third-party library versions that both parties rely on must be completely consistent
It must also be consistent, which can be solved using the trimpath parameter during compilation.
4. The plugin cannot be uninstalled after loading
These problems seem to be unsuccessful in a short period of time, or they cannot be solved. In short, Go plugin currently has few applications. After all, as a network programming language, it is very easy to update programs in an environment where containerization is popular, unless there are special needs.
Summarize
This is the end of this article about using Go Plugin to implement plug-in programming. For more related content on Go Plugin plug-in programming, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!