Example #1
0
func main() {
	c := gosafe.NewCompiler()
	c.Allow("math")
	cmd, err := c.Run("package main\nimport (\n\"fmt\"\n\"math\"\n)\nfunc main() { fmt.Println(math.Sin(10)) }\n")
	fmt.Println(cmd, ", ", err)
	c.Allow("fmt")
	cmd, err = c.Run("package main\nimport (\n\"fmt\"\n\"math\"\n)\nfunc main() { fmt.Println(math.Sin(10)) }\n")
	fmt.Println(cmd, ", ", err)
	cmd.Stdin.Close()
	b, _ := ioutil.ReadAll(cmd.Stdout)
	fmt.Print(string(b))
}
Example #2
0
func main() {
	c := gosafe.NewCompiler()
	c.Allow("github.com/zond/gosafe/child")
	c.Allow("fmt")
	c.Allow("time")
	if cmd, err := c.CommandFile("child.go"); err == nil {
		cmd.Timeout = time.Second / 2
		fetch(cmd)
		fetch(cmd)
		fetch(cmd)
		fmt.Println("sleeping...")
		time.Sleep(time.Second)
		fetch(cmd)
		fetch(cmd)
		fetch(cmd)
	} else {
		fmt.Println(err)
	}

}