dqtl46964 2016-08-30 01:41
浏览 29
已采纳

传递要在go应用程序中求值的表达式?

I have a go command line application, that has a search function to scan a list of objects (structs). I want to be able to invoke that search function, i.e. instead of just

mycmd search bob

Perhaps I can do:

mycmd search 'o.Name() == "bob"'
mycmd search 'len(o.PhoneNumbers()) > 1'
mycmd search 'strings.Index(o.Name(), "bob") >= 0'

Is it possible to process an expression like this, within a loop in your go code? If this is not possible, what is the simplest and most standard way to approach this in GO, without creating my own expression language/evaluator?

(I can see we have things like go-v8 and golua, however I'm not sure if this is the right way to think about it?)

NOTE: its a personal script, security concerns are out of scope.

  • 写回答

1条回答 默认 最新

  • duandi8544 2016-08-30 03:03
    关注

    Maybe you can use the text.template package for the evaluation of your input.
    And later parse the output of it for the result of the entered expression.

    This is was a few Go build tools do. For example docker inspect uses this for formatting its output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备