Plugin, shared variables

I’m using plugin but is not documentated so is a little tricky to understand

There is a way to share values between the main program that calls the plugin and the plugin itself? Or something to send variables from plugin to main program

Hey @Nhoya,

Here’s some docs for the plugin package: https://golang.org/pkg/plugin/.

At the bottom of the page you can see where the plugin that was looked up is converted to a func() type and then called like the following: f.(func())(), which is where I would assume you would pass your values into if the type accepts arguments.

I haven’t personally used the plugin package yet, but the example there should make some sense hopefully :slight_smile:

1 Like

thanks for your reply @radovskyb but this actually allow me to pass arguments to the plugin but what i really want is pass arguments from plugin to main program (without ending the plugin execution)

Do you mean something like this which I’ve edited from one of the example’s on that page:

// Lookup variable named `V` from inside of the plugin.
v, err := p.Lookup("V")
if err != nil {
	panic(err)
}
// Store the value of the plugin's integer variable `V` into `i`.
i := *v.(*int)

Don’t forget that since this is the description for a symbol: “A Symbol is a pointer to a variable or function.”, you should be able to just store the result of a function call or even just use a variable directly looked up from the plugin, in your main program.

Yea i saw this, but in this way i haven’t a “real time” way to see the result, let’s make a real example

i’v a plugin that is running an infinite loop, in this loop the plugin makes some controls e.g. if a file exists it should send a “signal” (e.g. changing a variable) to the main without terminating it’s execution and the main program should receive it.

I can’t make the main program read each n seconds because i will lose the real time effect (there is a delay for each control)

I do have one possible idea how you could achieve this, but I can’t test it as I’m not running linux at the moment (plugin’s only work on linux currently). If you are trying to signal from the plugin to the main program, have you tried returning a channel from a function call from the plugin and just using that?

I would really suggest that you think about why you are using plugins in the first place though, for example, do you actually need to be using plugins for your use case? [quote=“Nhoya, post:5, topic:4841”]
e.g. if a file exists it should send a “signal” (e.g. changing a variable) to the main without terminating it’s execution and the main program should receive it.
[/quote]

The above example is much better off just being written into your actual program and not through a plugin.

turning my program into a monolitic one will solve this issue but i actually need Plugin because in that way i can add modularity so if i need other things i can add a new plugin with other functions

Sounds like you should be separating concerns a little bit better with separated packages and using more interface definitions to be able to swap out parts of your code rather than using plugins to solve this problem.

Is there anything in particular that you’re trying to solve that you feel that you can’t accomplish without plugins at the moment?

P.S. This is only my opinion

what i’v in mind is a “multitasks” program, each task is a module, each module will communicate with the “main one” (the process that start all the plugins) to send status notification, just this

If you have others solutions i’ll be happy to listen them, i’m always open to suggestions

The fact is that using plugin i can just “remove” one task removing the plugin and rebuilding the main program

Ah ok, I see.

I guess my question now would be whether or not you actually think that it’s going to be necessary for your use case to be using hot plugin swapping, rather than just rebuilding and redeploying regular code or package changes?

There are things out there right now that you can use for live reloading of servers and such if that’s an interest to you, for example: https://github.com/codegangsta/gin is one that comes to mind.

Because is more user friendly, but i think at this point i’ll start rewrite everthing to just load modules on config-file (but this will result on a big size binary)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.