Created
August 30, 2016 02:02
-
-
Save matiasinsaurralde/85df9adc22b12c4406e0c9ae1a2fda23 to your computer and use it in GitHub Desktop.
otto-fs.go
This file contains hidden or 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 ( | |
"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) | |
} |
This file contains hidden or 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
hello | |
world |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment