在Go中运行用户提交的代码

I am working on an application which allows users to compare the execution of different string comparison algorithms. In addition to several algorithms (including Boyer-Moore, KMP, and other "traditional" ones) that are included, I want to allow users to put in their own algorithms (these could be their own algorithms or modifications to the existing ones) to compare them.

Is there some way in Go to take code from the user (for example, from an HTML textarea) and execute it?

More specifically, I want the following characteristics:

  • I provide a method signature and they fill in whatever they want in the method.
  • A crash or a syntax error in their code should not cause my whole program to crash. It should instead allow me to catch the error and display an error message.

(In this case, I am not worried about security against malicious code because users will only be executing my program on their own machines, so security is their own responsibility.)

If it is not possible to do this natively with Go, I am open to embedding one of the following languages to use for the comparison functions (in order of preference): JavaScript, Python, Ruby, C. Is there any way to do any of those?

A clear No.

But you can do fancy stuff: Why not recompile the program including the user provided code? Split the stuff into two: One driver which collects user code, recompiles the actual code, executes the actual code and reports the outcome.

Including other interpreters for other languages can be done, e.g. Otto is a Javascript interpreter. (C will be hard :-)

Have you considered doing something similar to the gopherjs playground? According to this, the compilation is being done client-side.