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 1/20
    Go 1.8 Plugins
    Ian Lewis

    View Slide

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

    View Slide

  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
    }

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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!

    View Slide

  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)

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

  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)

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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)
    [email protected] (mailto:[email protected])

    View Slide