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.
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.