Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting Plugged in with Go 1.8 Plugins @ GopherCon India

Ian Lewis
February 25, 2017

Getting Plugged in with Go 1.8 Plugins @ GopherCon India

Go 1.8 will include a plugin package that allows you to define plugins that can be loaded at runtime. This talk will cover plugins in detail, discuss some use cases, and best practices for using the package.

Ian Lewis

February 25, 2017
Tweet

More Decks by Ian Lewis

Other Decks in Technology

Transcript

  1. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 3/20 Go 1.5 Go 1.5

    introduced shared libraries using the -buildmode=shared option. // filename: calc.go package calc func Sum(x, y int) int { return x + y }
  2. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 4/20 Go shared libraries First

    you need to build Go stdlib as a shared library $ go install -buildmode=shared -linkshared std Build the shared library calc$ go install -buildmode=shared -linkshared calc
  3. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 5/20 Go shared libraries //

    package: cashier // filename: main.go package main import "calc" import "fmt" func main() { fmt.Println("Cashier Application") fmt.Printf("Result: %d\n", calc.Sum(5, 10)) } & link it to another app cashier$ go build -linkshared
  4. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 6/20 Go shared libraries Good

    for distributing binary code as a re-usable library Still need to link at compile time
  5. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 8/20 Prior Art Exec Plugins

    (https://github.com/kelseyhightower/kube-cert-manager/blob/master/docs/plugins.md) Hashicorp Plugins (https://github.com/hashicorp/go-plugin)
  6. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 9/20 Go Plugins Write a

    main package without main() func package main import "fmt" var V int func F() { fmt.Printf("Hello, number %d\n", V) }
  7. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 11/20 Load a Plugin Load

    the plugin by passing the path to the .so to plugin.Open() func main() { p, err := plugin.Open("adder-plugin/adder-plugin.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" }
  8. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 13/20 Use Cases Exposing API

    w/o source code (closed source) Abstract Backends (e.g. Cloud providers)
  9. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 14/20 Pros/Cons Pro: Easy loading

    at runtime Pro: Single process to maintain/monitor Con: Can't unload plugin, no hot reloading Con: It's new
  10. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 18/20 Wrapping Up Moral of

    the Story: Testing is contribution! Provide ways for your users to extend Know the tradeoffs
  11. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 19/20 Thank you Ian Lewis

    Gopher/Kubernaut@Google (mailto:Gopher/Kubernaut@Google) @IanMLewis (http://twitter.com/IanMLewis) [email protected] (mailto:[email protected])