Why do plugins require a main package?

191 views
Skip to first unread message

Craig Peterson

unread,
Nov 30, 2016, 2:13:10 PM11/30/16
to golang-nuts
From the 1.8 plugin docs

A plugin is a Go main package with exported functions and variables

This creates a bit of a headache for me. My current setup is that a plugin is a simple library package like so:

package someLibrary

import "gihub.com/someapp/pluginRegistry"


func init
(){
   pluginRegistry
.Register(myPlugin)
}

This works well, but requires being compiled in (usually with an
import _ "someLibrary"
in main somewhere)

I would also like to add support for these plugins to be compiled independently and loaded at runtime. 

In order to do with the plugin package, I need to write a little shim package that is literally:

package main


import _ "someLibrary"


func main
(){}

and build that as a plugin. 

This feels like unnecessary boilerplate to me. Can anyone explain what are the technical reasons for requiring a main package? Could the compiler not generate a main package shim like this if you try to build a library package with -buildmode=plugin ?

Lars Tørnes Hansen

unread,
Nov 30, 2016, 2:37:23 PM11/30/16
to golang-nuts
I would pay attention to with exported functions and variables.


Fx

func SomeFunc() {}

Ian Lance Taylor

unread,
Nov 30, 2016, 3:52:53 PM11/30/16
to Craig Peterson, golang-nuts
On Wed, Nov 30, 2016 at 9:47 AM, Craig Peterson
<peterso...@gmail.com> wrote:
>
> This feels like unnecessary boilerplate to me. Can anyone explain what are
> the technical reasons for requiring a main package? Could the compiler not
> generate a main package shim like this if you try to build a library package
> with -buildmode=plugin ?

The technical limitation is that there needs to be an entry point that
will run all the initializers. And, we want to permit a plugin to
include multiple unrelated packages, so there needs to be a single
point that lists them all.

The same issue arises for -buildmode=c-archive and c-shared.

We could arrange for the go tool (probably not the compiler itself) to
optionally auto-generate a main package. Want to write a patch?

Ian

Craig Peterson

unread,
Nov 30, 2016, 3:55:53 PM11/30/16
to golang-nuts, peterso...@gmail.com

We could arrange for the go tool (probably not the compiler itself) to
optionally auto-generate a main package.  Want to write a patch?


Yes. Yes I do. Not sure exactly where to start, but I will start digging. I will file an issue to track I suppose. 

Craig Peterson

unread,
Nov 30, 2016, 3:59:45 PM11/30/16
to golang-nuts
It appears this is not a hard requirement. In my tests I can successfully load a compiled plugin with no exported functions or variables.
Reply all
Reply to author
Forward
0 new messages