Skip to content

Instantly share code, notes, and snippets.

@matiasinsaurralde
Created August 30, 2016 02:02
Show Gist options
  • Save matiasinsaurralde/85df9adc22b12c4406e0c9ae1a2fda23 to your computer and use it in GitHub Desktop.
Save matiasinsaurralde/85df9adc22b12c4406e0c9ae1a2fda23 to your computer and use it in GitHub Desktop.
otto-fs.go
package main
import (
"github.com/robertkrimen/otto"
"io/ioutil"
)
func main() {
vm := otto.New()
fsModuleDef := map[string]func(otto.FunctionCall) otto.Value{
"readFileSync": func(call otto.FunctionCall) otto.Value {
var filename string
filename, _ = call.Argument(0).ToString()
fileData, _ := ioutil.ReadFile(filename)
output, _ := vm.ToValue(string(fileData))
return output
},
}
fsModule, _ := vm.ToValue(fsModuleDef)
vm.Set("fs", fsModule)
var script = `
var output = fs.readFileSync('test.txt')
console.log("File data:")
console.log(output)
`
vm.Run(script)
}
hello
world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment