Skip to content

Instantly share code, notes, and snippets.

@Focinfi
Last active April 19, 2016 02:36
Show Gist options
  • Save Focinfi/ef435388067bbbd1526cd5dc6c06ae07 to your computer and use it in GitHub Desktop.
Save Focinfi/ef435388067bbbd1526cd5dc6c06ae07 to your computer and use it in GitHub Desktop.
call qlang functions in golang
package main
import (
"log"
"os"
"qlang.io/exec.v2"
"qlang.io/qlang.v2/qlang"
qall "qlang.io/qlang/qlang.all" // 导入 builtin 包
)
var code = `
greet = fn(a, b) {
return a + b
}
`
func main() {
qall.InitSafe(false)
lang, err := qlang.New(qlang.InsertSemis)
lang.SetLibs(os.Getenv("QLANG_PATH"))
if err != nil {
log.Println(err)
return
}
err = lang.SafeExec([]byte(code), "")
if err != nil {
log.Println(err)
return
}
greet, ok := lang.Var("greet")
if ok {
if g, ok := greet.(*exec.Function); ok {
log.Println(g.Call(1, 2))
} else {
log.Printf("%T", greet)
}
} else {
log.Println("Can not get greet var in code")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment