douhang1913 2014-11-18 16:17
浏览 123
已采纳

在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?

  • 写回答

2条回答 默认 最新

  • dongleiqiao4906 2014-11-18 16:24
    关注

    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 :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计