risor-io/risor

How Risor work with Go program? Or just a script language?

Closed this issue · 5 comments

How Risor work with Go program? Or just a script language?

myzie commented

You can embed it in existing Go programs. Here's an example of how you can use it to wrap a Go service struct:

https://github.com/risor-io/risor/blob/main/examples/go/struct/main.go

Does that help?

You can embed it in existing Go programs. Here's an example of how you can use it to wrap a Go service struct:

https://github.com/risor-io/risor/blob/main/examples/go/struct/main.go

Does that help?

Yes, I need it. That's good enough.

Summary: there are two methods to use Risor with Go program.

  1. Write code inside Risor. This method need register all object and function/method into Risor
    with risor.WithBuiltins(), and use risor.Eval to do all things.
  2. Use Risor inside your Go project. Use Risor to exec piece of codes, and handle the result of risor.Eval.

And I guess there are some pros and cons for each method. (I am not judge Risor, I really need Risor.)

Method Pros Cons
1 1. Easy to use 1. Low performance
2. More runtime error
2 1. Closer native Go performance 1. Need handle the result of risor.Eval

Thanks for all your great work.

myzie commented

I definitely agree that there a couple different ways to integrate Risor into a larger project.

In terms of the two options you mention, I think I follow what you mean but I want to make sure. Basically, in your "method 1" a Risor script acts as the main program, presumably executes whatever run loop is present for the program, and calls into Go objects that you've injected into Risor to do anything custom. While in "method 2", your main program is written Go and you use risor.Eval for specific processing tasks only. Is that right?

This does seem like a useful way to think about it, and yes I think each approach could be useful depending on your program.

A third approach ("method 3"), which might not be relevant to you, is to write your new program or script entirely in Risor script, and use the off-the-shelf Risor CLI to run it. This is just like how a Python script you write is run by the python binary.

I do think it will be common to use your "method 2". And "method 3" may be more common than "method 1".

Are we thinking along the same lines?

Yep, we are. As you've said, "method 3" may be more common than "methond 1". I do intend to use "method 2" to intergrate Risor with a existing project.

myzie commented

I'm going to mark this closed, but please let me know how the integration goes!