-
I want to use expr to execute a function with no return value, for example to create a file with it, can you provide an example of this? I've been googling for a long time but I can't find any information about it, thanks in advance! The following code will panic with func TestDocGen(t *testing.T) {
// "github.com/carmel/gooxml/document"
env := document.New()
code := `
let para = AddParagraph();
let run = para.AddRun();
run.AddText("document title");
SaveToFile("test.docx")
`
program, err := expr.Compile(code, expr.Env(env))
if err != nil {
panic(err)
}
output, err := expr.Run(program, env)
if err != nil {
panic(err)
}
fmt.Println(output)
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Yes, right now, it is not possible to do that. As Expr is only for expressions, and all expressions should return a value. You can work around this restriction by returning something from functions and discarding the results. For example return let para = AddParagraph();
let run = para.AddRun();
run.AddText("document title")
and SaveToFile("test.docx") Or wrap in array: [
run.AddText("document title"),
and SaveToFile("test.docx"),
] I'm thinking to add an option to allow calling functions as statements. Also, I'm thinking of adding if/else into the language to the already existing |
Beta Was this translation helpful? Give feedback.
Yes, right now, it is not possible to do that. As Expr is only for expressions, and all expressions should return a value.
You can work around this restriction by returning something from functions and discarding the results. For example return
true
and you can this:Or wrap in array:
I'm thinking to add an option to allow calling functions as statements. Also, I'm thinking of adding if/else into the language to the already existing
cond ? yes : no
.