-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathplugin.htm
194 lines (114 loc) · 6.57 KB
/
plugin.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!DOCTYPE html>
<html lang="en">
<head profile="http://a9.com/-/spec/opensearch/1.1/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="../assets/site.css" rel="stylesheet">
<title>plugin</title>
<meta name="twitter:title" content="Package plugin">
<meta property="og:title" content="Package plugin">
<meta name="description" content="Package plugin implements loading and symbol resolution of Go plugins.">
<meta name="twitter:description" content="Package plugin implements loading and symbol resolution of Go plugins.">
<meta property="og:description" content="Package plugin implements loading and symbol resolution of Go plugins.">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@golang">
</head>
<body>
<div class="container">
<h2 id="pkg-overview">package plugin</h2>
<p><code>import "plugin"</code>
<p>
Package plugin implements loading and symbol resolution of Go plugins.
</p>
<p>
Currently plugins only work on Linux.
</p>
<p>
A plugin is a Go main package with exported functions and variables that
has been built with:
</p>
<pre>go build -buildmode=plugin
</pre>
<p>
When a plugin is first opened, the init functions of all packages not
already part of the program are called. The main function is not run.
A plugin is only initialized once, and cannot be closed.</p>
<h3 id="pkg-index" class="section-header">Index <a class="permalink" href="#pkg-index">¶</a></h3>
<ul class="list-unstyled">
<li><a href="#Plugin">type Plugin</a></li>
<ul>
<li><a href="#Open">func Open(path string) (*Plugin, error)</a></li>
<li><a href="#Plugin.Lookup">func (p *Plugin) Lookup(symName string) (Symbol, error)</a></li>
</ul>
<li><a href="#Symbol">type Symbol</a></li>
</ul>
<span id="pkg-examples"></span>
<h4 id="pkg-files">
<a href="https://github.com/golang/go/blob/master/src/plugin/">Package Files</a>
<a class="permalink" href="#pkg-files">¶</a>
</h4>
<p><a href="https://github.com/golang/go/blob/master/src/plugin/plugin.go">plugin.go</a> <a href="https://github.com/golang/go/blob/master/src/plugin/plugin_dlopen.go">plugin_dlopen.go</a> </p>
<h3 id="Plugin" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/plugin/plugin.go#L20">Plugin</a> <a class="permalink" href="#Plugin">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=Plugin&pkg=plugin&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/plugin/plugin.go#L20">❖</a><pre>type Plugin struct {
<span class="com">// contains filtered or unexported fields</span>
}</pre></div><p>
Plugin is a loaded Go plugin.
</p>
<h4 id="Open" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/plugin/plugin.go#L29">Open</a> <a class="permalink" href="#Open">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=Open&pkg=plugin&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/plugin/plugin.go#L29">❖</a><pre>func Open(path <a href="/builtin#string">string</a>) (*<a href="#Plugin">Plugin</a>, <a href="/builtin#error">error</a>)</pre></div><p>
Open opens a Go plugin.
If a path has already been opened, then the existing *Plugin is returned.
It is safe for concurrent use by multiple goroutines.
</p>
<h4 id="Plugin.Lookup" data-kind="m">func (*Plugin) <a title="View Source" href="https://github.com/golang/go/blob/master/src/plugin/plugin.go#L37">Lookup</a> <a class="permalink" href="#Plugin.Lookup">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=Plugin%2FLookup&pkg=plugin&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/plugin/plugin.go#L37">❖</a><pre>func (p *<a href="#Plugin">Plugin</a>) Lookup(symName <a href="/builtin#string">string</a>) (<a href="#Symbol">Symbol</a>, <a href="/builtin#error">error</a>)</pre></div><p>
Lookup searches for a symbol named symName in plugin p.
A symbol is any exported variable or function.
It reports an error if the symbol is not found.
It is safe for concurrent use by multiple goroutines.
</p>
<h3 id="Symbol" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/plugin/plugin.go#L73">Symbol</a> <a class="permalink" href="#Symbol">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=Symbol&pkg=plugin&repo=">Uses</a></h3>
<div class="decl" data-kind="m"><a title="View Source" href="https://github.com/golang/go/blob/master/src/plugin/plugin.go#L73">❖</a><pre>type Symbol interface{}</pre></div><p>
A Symbol is a pointer to a variable or function.
</p>
<p>
For example, a plugin defined as
</p>
<pre>package main
// // No C code needed.
import "C"
import "fmt"
var V int
func F() { fmt.Printf("Hello, number %d\n", V) }
</pre>
<p>
may be loaded with the Open function and then the exported package
symbols V and F can be accessed
</p>
<pre>p, err := plugin.Open("plugin_name.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"
</pre>
<div id="x-footer" class="clearfix">
<div class="container">
<a href="http://studygolang.com/" target="_blank">Go语言中文网</a>
<span class="text-muted">|</span> <a href="http://golang.org/" target="_blank">Go Language</a>
<span class="pull-right"><a href="#">Back to top</a></span>
</div>
</div>
<script src="../assets/jquery-2.0.3.min.js"></script>
<script src="../assets/bootstrap.min.js"></script>
<script src="../assets/site.js"></script>
</body>
</html>