Getting Plugged in with Go 1.8 Plugins @ GopherCon India

01dc8e954957a10b428aa60b28c89d52?s=47 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.

01dc8e954957a10b428aa60b28c89d52?s=128

Ian Lewis

February 25, 2017
Tweet

Transcript

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

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

  3. 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 }
  4. 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
  5. 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
  6. 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
  7. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 7/20 Go 1.8 New plugin

    package New -buildmode=plugin Load shared code at runtime!
  8. 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)
  9. 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) }
  10. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 10/20 Build a Plugin Build

    to an .so file $ go build -buildmode=plugin adder
  11. 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" }
  12. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 12/20 Build the Main App

    Apps that use plugins can be built normally $ go build
  13. 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)
  14. 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
  15. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 15/20 Testing

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

  17. 2/25/2017 Go 1.8 Plugins http://localhost:3999/go-1.8-plugins.slide#1 17/20 More Testing

  18. 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
  19. 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) ianlewis@google.com (mailto:ianlewis@google.com)