Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Go Plugin to realize plug-in programming

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to use Go Plugin to achieve plug-in programming, the article is rich in content and professional analysis and description for you, I hope you can get something after reading this article.

Preface

When it comes to plug-ins, many people are no stranger to them. generally speaking, plug-in has several advantages, one is to increase program expansibility and enrich functions. In addition, you can also achieve hot updates, some large applications, easily several GB installers, if a small update needs to re-download the entire program, then we can plug in frequently updated modules, so that when updating, you only need to download a small update file. For example, usually our Chrome browser will install some plug-ins, you can expand the browser to achieve more functions, but also the flexibility to install and uninstall.

Golang provides a Plugin mechanism after version 1.8, which can dynamically load so files and achieve 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. Quick start

Plug-in code is no different from ordinary code, except at compile time, but requires that there must be only one main package

Package mainvar Name = "Plugin Name" func GetName () string {return Name}

Using go build-buildmode=plugin compilation, you 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 plug-in has been opened, the existing plugin will be returned.

two。 Use Lookup to find the variable or function to be called. The name must begin with an uppercase case.

3. Called after assertion

Func main () {/ / Open the loading plug-in. The parameter is the storage location of the plug-in. Can be relative path open, err: = plugin.Open ("/ home/jwang/Documents/plg.so") if err! = nil {panic (err)} / / lookup identifier lookup, err: = open.Lookup ("GetName") if err! = nil {panic (err)} res: = lookup. (func () string) () fmt.Printf ("% v\ n", res) name Err: = open.Lookup ("Name") if err! = nil {panic (err)} fmt.Printf ("% v\ n", * name. (* string))}

As you can see from the above code, the plug-in is used in a very simple and easy-to-understand way.

In general, in order to achieve plug-in, you can define some interfaces in advance, and then let the plug-in to implement these interfaces, so as to ensure consistency, but the definition of the interface can not be written in the plug-in package or call package. At this point, you need to define a special public package and write the definition of the interface in it, so that both the plug-in package and the call package can be referenced.

two。 Matters needing attention

The reason why this plug-in scheme is immature is mainly due to the strong dependency between the main program and the plug-in, such as:

1. The compiled GO version must be exactly the same

two。 The versions of public third-party libraries that both parties depend on must be exactly the same.

3.GOPATH also needs to be consistent, which can be solved by using the trimpath parameter at compile time

4. The plug-in cannot be unloaded after loading

It seems that the authorities have no intention of solving these problems in a short period of time, or that they cannot be solved. In a word, there are few applications of Go plugin at present. After all, as a network programming language, it is very easy to update programs in a containerized environment, unless there are special needs.

The above is the editor for you to share how to use Go Plugin to achieve plug-in programming, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report