-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to run gomacro inside an existing application [solved] #13
Comments
Yes to both questions :) Running gomacro inside an existing Go application is very easy:
For performance, you'd better reuse the Starting an interactive prompt (a REPL) is only slightly more complicated - tell me if you need instructions for that. To inject your application's function into gomacro, write an You can either write such an
It will create a file P.S. if you distribute your application, remember that Gomacro has LGPL license and you must write/tell that to your users, adding where they can download Gomacro source code from. UPDATE license is now the more relaxed MPL 2.0 |
I hope the answer is sufficiently clear and detailed. Closing the issue, feel free to reply if you need further clarifications, and I will reopen it. |
Reopening to make it more visible to new users |
Hey, first off, thanks for a great software package! Secondly: how would you go about just invoking a function in the interpreter, that would take some go-type (interface, actually) as a parameter (and likely returns something). For example:
Am I going in the correct direction? I'm missing something which would be basically an utility function for a function call, something like |
Hi,
The first technique then calls the function with
The second technique instead extracts the function from the interpreter and uses it as any other (compiled) function:
Actually, the function |
gomacro continues to amaze me. Is there any way to have the |
Is there an inverse of That is, in my host code I have say a Go struct value (of a type precompiled into in x_package.go), and now I want to inject that value into the gomacro interpreter global variables without having the serialize it into a string and use Eval. Create a new global variable inside gomacro with the value pointing to (or a fast binary copy of) the value from the host. update: another way of asking this would be: if I want to add x long after init() time, and I do
|
The content of You can instead use If you need to exchange a lot of different values between the interpreter and compiled Go, it's more efficient to use
|
This is so cool. Thanks @cosmos72. I was able to inject a global x with interp.DeclVar(). But to inject x into package y, so far I did need to have a call to
prior to my
call. I'm not sure if this is intended? I tried
without luck. Let me know if there is a shortcut way that uses DeclVar() alone. Thanks again. update: arg. sorry -- my updates to the package variable aren't working. It is always that initial value. |
if the global variable is in a different package, you cannot directly
or to import the package (if not done already), change to that package, declare the variable, then change back (more verbose):
In both cases, to extract the (settable) variable
as shown below:
Since you are trying to exchange data between compiled Go and the interpreter, the very commented examples in |
awesome. Thanks Massimiliano! For anyone else reading, this was my final flow for updating variable
|
Good :) Some minor considerations:
In case you choose the first, the
in this way, the variable visible inside the interpreter will be simply the compiled 'x' - any changes to it will be visible from both sides - so you can just write, for example:
On the other hand, if you choose Summarizing, there are two alternatives. They both require to generate beforehand the file ============= alternative 1 =================
============= alternative 2 =================
|
…ables between compiled Go and the intepreter. Created from the outcome of cooperative discussion in #13, thanks to @glycerine for it.
I created the file _example/glycerine1/main.go containing the complete, commented, working code for both techniques to share variables between compiled code and gomacro interpreter. Thanks for the useful discussion :) |
…ables between compiled Go and the intepreter. Created from the outcome of cooperative discussion in #13, thanks to @glycerine for it.
@cosmos72 Between this and the |
I did not understand the question: |
@cosmos72 commented on Aug 27, 2020, 12:11 PM GMT+4:30:
I'm used to IPython's embed, which drops you into a REPL where you can access the local state, call functions, etc. Similar to |
I guess you're talking about the statement Then it's quite different from the example above: executing The example above instead explains how to exchange data between interpreted and compiled Go code by compiling and executing some Go code - definitely not something you can do interactively. I hope this helps clarifying the difference. |
Do you think it'd be possible to go even further and run something like gophernotes from within an app? (to get a notebook interface available inside a running application) |
Good question :) |
Ideally with the application's functions available. Do you think that can be done ?
The text was updated successfully, but these errors were encountered: