forked from vadv/gopher-lua-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
45 lines (39 loc) · 911 Bytes
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package ioutil
import (
"log"
lua "github.com/yuin/gopher-lua"
)
// ioutil.read_file(filepath)
func ExampleReadFile() {
state := lua.NewState()
Preload(state)
source := `
local file = io.open("./test/file.data", "w")
file:write("content of test file", "\n")
file:close()
local ioutil = require("ioutil")
local result, err = ioutil.read_file("./test/file.data")
if err then error(err) end
print(result)
`
if err := state.DoString(source); err != nil {
log.Fatal(err.Error())
}
// Output:
// content of test file
}
// ioutil.write_file(filepath)
func ExampleWriteFile() {
state := lua.NewState()
Preload(state)
source := `
local ioutil = require("ioutil")
local err = ioutil.write_file("./test/file.data", "content of test file")
if err then error(err) end
`
if err := state.DoString(source); err != nil {
log.Fatal(err.Error())
}
// Output:
//
}