Last active
April 19, 2016 02:36
-
-
Save Focinfi/ef435388067bbbd1526cd5dc6c06ae07 to your computer and use it in GitHub Desktop.
call qlang functions in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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