Golang1.8 officially supports loading dynamic libraries, and looks powerful. The document describes the plugin functionality as a process concurrency security, and supports advanced data types (including Chan) without having to write any C code (which is more or less needed to be written before).
Here are some demos to see how to get started with Golang plugin features
The following is a simple plugin code:
Package main
///No C code needed.
Import "C"
import (
"FMT"
)
var V int
func F () {fmt. Printf ("Hello, Number%d\n", V)}
This is an example provided by the authorities and we will expand it later.
Go build-buildmode=plugin main.go //main.go is plugin file name, you can change it to another name
You will see one more main.so dynamic library.
And then we start calling this dynamic library
Package main
Import (
"plugin"
)
func main () {
p, err: = plugin. Open ("main.so")
If Err!= nil {
panic (err)
}
V, err: = P.lookup ("V")
if Err!= nil {
Panic ( ERR)
}
F, err: = P.lookup ("f")
If Err!= nil {
panic (err)
}
*v. (*int) = 7
F. (func ()) ()
Normally compile this go file and then execute it to see that the screen will output
Hello, number 7
This is just the simplest form, and we add some difficulty, using some complex data types. working with Complex types
We customize a structure
Package data
type VS struct {
Name string
age int
School string
}
Note here that you have to put the structure in another package, otherwise build plugin, will hold the structure of the body can not find.
And then we start modifying the plugin code.
Package main
///No C code needed.
Import "C"
import (
"FMT"
"Temp/data"
)
var V int
func F () {fmt. Printf ("Hello, Number%d\n", V)}
var Vs data. VS
func ComplexType () {
fmt. Println (Vs.name, Vs.age, Vs.school)
}
We declared the VS struct, then added a function to use the struct body (the need to recompile plugin). The caller also needs to synchronize the modifications
Package main
Import (
"plugin"
"Temp/data"
)
func main () {
p, err: = plugin. Open ("main.so")
If Err!= nil {
panic (err)
}
V, err: = P.lookup ("V")
if Err!= nil {
Panic (err)
}
F, err: = P.lookup ("F")
If Err!= nil {
panic (err)
}
*v. (*int) = 7
F. (func ()) ()//prints "Hello, Number 7 "
vs, err: = P.lookup (" vs ")
If Err!= nil {
panic (err)
}
ct, err: = P.lookup (" Complextyp E ")
if Err!= nil {
panic (err)
}
*vs. (*data. VS) = data. vs{
Name: "DATA", age
: One ,
School: "BEIDA",
}
Ct. (func ()) ()
}
In main we build a VS structure and assign values to each property. If normal, you will see the following information
Hello, number 7
DATA one BEIDA
So far, we've been able to use advanced complex structures in plugin, and we can say we've already met 50% requirements. But exploration is endless, if calls and plugin need to communicate with each other. Now let's see if plugin supports Chan. Support Chan
First declare a global Chan
Package data
type VS struct {
Name string
age int
School string
}
var Msg = make (chan VS)
Not only does it declare a global Chan, but it is also an advanced type of Chan. The following use of Chan in plugin
Package main
///No C code needed.
Import "C"
import (
"FMT"
"Temp/data"
)
var V int
func F () {fmt. Printf ("Hello, Number%d\n", V)}
var Vs data. VS
func ComplexType () {
fmt. Println (Vs.name, Vs.age, vs.school)
vs.age = vs.age * 4
data. MSG <-Vs
}
Plugin receives the VS data, enlarges the age by 4 times times and throws it out (regardless of where it is thrown) (requires recompilation of the plugin). Then modify the call function:
Package main
Import (
"plugin"
"Temp/data"
)
func main () {
p, err: = plugin. Open ("main.so")
If Err!= nil {
panic (err)
}
V, err: = P.lookup ("V")
if Err!= nil {
Panic (err)
}
F, err: = P.lookup ("F")
If Err!= nil {
panic (err)
}
*v. (*int) = 7
F. (func ()) ()//prints "Hello, Number 7 "
vs, err: = P.lookup (" vs ")
If Err!= nil {
panic (err)
}
ct, err: = P.lookup (" Complextyp E ")
if Err!= nil {
panic (err)
}
*vs. (*data. VS) = data. vs{
Name: "DATA", age
: One ,
School: "BEIDA",
go
Ct. (func ()) ()
Select { Case
m: = <-data. MSG:
println (m.age)
}
}
In fact, it is in the main function to accept the thrown back vs structure body. After compiling and then executing, you can see
Hello, number 7
DATA one BEIDA
44
OK, perform normally and meet expectations. Here first to provide plugin how to use, we later analyze how Golang is to achieve these functions.