blob: 66cf2f98d459adbbecb61fe1c3e2de3e294e7d92 [file] [log] [blame]
David Crawshaw0cbb12f2016-08-26 08:50:50 -04001// Copyright 2016 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Package plugin implements loading and symbol resolution of Go plugins.
6//
7// Currently plugins only work on Linux.
8//
9// A plugin is a Go main package with exported functions and variables that
10// has been built with:
11//
12// go build -buildmode=plugin
13//
14// When a plugin is first opened, the init functions of all packages not
15// already part of the program are called. The main function is not run.
16// A plugin is only initialized once, and cannot be closed.
17package plugin
18
19// Plugin is a loaded Go plugin.
20type Plugin struct {
21 name string
22 loaded chan struct{} // closed when loaded
23 syms map[string]interface{}
24}
25
26// Open opens a Go plugin.
27func Open(path string) (*Plugin, error) {
28 return open(path)
29}
30
31// Lookup searches for a symbol named symName in plugin p.
32// A symbol is any exported variable or function.
33// It reports an error if the symbol is not found.
34func (p *Plugin) Lookup(symName string) (Symbol, error) {
35 return lookup(p, symName)
36}
37
38// A Symbol is a pointer to a variable or function.
39//
40// For example, a plugin defined as
41//
42// package main
43//
44// // // No C code needed.
45// import "C"
46//
47// import "fmt"
48//
49// var V int
50//
51// func F() { fmt.Println("Hello, number %d", V) }
52//
53// may be loaded with the Open function and then the exported package
54// symbols V and F can be accessed
55//
56// p, err := plugin.Open("plugin_name.so")
57// if err != nil {
58// panic(err)
59// }
60// v, err := p.Lookup("V")
61// if err != nil {
62// panic(err)
63// }
64// f, err := p.Lookup("F")
65// if err != nil {
66// panic(err)
67// }
68// *v.(*int) = 7
69// f.(func())() // prints "Hello, number 7"
70type Symbol interface{}