github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/plugin/plugin.go (about) 1 // 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 // A plugin is a Go main package with exported functions and variables that 8 // has been built with: 9 // 10 // go build -buildmode=plugin 11 // 12 // When a plugin is first opened, the init functions of all packages not 13 // already part of the program are called. The main function is not run. 14 // A plugin is only initialized once, and cannot be closed. 15 // 16 // The plugin support is currently incomplete, only supports Linux, 17 // and has known bugs. Please report any issues. 18 package plugin 19 20 // Plugin is a loaded Go plugin. 21 type Plugin struct { 22 pluginpath string 23 loaded chan struct{} // closed when loaded 24 syms map[string]interface{} 25 } 26 27 // Open opens a Go plugin. 28 // If a path has already been opened, then the existing *Plugin is returned. 29 // It is safe for concurrent use by multiple goroutines. 30 func Open(path string) (*Plugin, error) { 31 return open(path) 32 } 33 34 // Lookup searches for a symbol named symName in plugin p. 35 // A symbol is any exported variable or function. 36 // It reports an error if the symbol is not found. 37 // It is safe for concurrent use by multiple goroutines. 38 func (p *Plugin) Lookup(symName string) (Symbol, error) { 39 return lookup(p, symName) 40 } 41 42 // A Symbol is a pointer to a variable or function. 43 // 44 // For example, a plugin defined as 45 // 46 // package main 47 // 48 // import "fmt" 49 // 50 // var V int 51 // 52 // func F() { fmt.Printf("Hello, number %d\n", V) } 53 // 54 // may be loaded with the Open function and then the exported package 55 // symbols V and F can be accessed 56 // 57 // p, err := plugin.Open("plugin_name.so") 58 // if err != nil { 59 // panic(err) 60 // } 61 // v, err := p.Lookup("V") 62 // if err != nil { 63 // panic(err) 64 // } 65 // f, err := p.Lookup("F") 66 // if err != nil { 67 // panic(err) 68 // } 69 // *v.(*int) = 7 70 // f.(func())() // prints "Hello, number 7" 71 type Symbol interface{}