Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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 }

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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!

Slide 8

Slide 8 text

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)

Slide 9

Slide 9 text

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) }

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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" }

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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)

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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])