-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioutil.go
48 lines (44 loc) · 1.01 KB
/
ioutil.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
46
47
48
package main
import (
"io"
"os"
lua "github.com/yuin/gopher-lua"
)
func ioutilWriteText(l *lua.LState) int {
dest := l.CheckString(1)
input := l.CheckString(2)
f, err := os.OpenFile(dest, os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
l.RaiseError("unable to open file: %v", err)
}
defer f.Close()
bytes, err := io.WriteString(f, input)
if err != nil {
l.RaiseError("unable to write file: %v", err)
}
err = f.Sync()
if err != nil {
l.RaiseError("unable to sync file: %v", err)
}
l.Push(lua.LNumber(float64(bytes)))
return 1
}
func ioutilAppendText(l *lua.LState) int {
dest := l.CheckString(1)
input := l.CheckString(2)
f, err := os.OpenFile(dest, os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
l.RaiseError("unable to open file: %v", err)
}
defer f.Close()
bytes, err := io.WriteString(f, input)
if err != nil {
l.RaiseError("unable to write file: %v", err)
}
err = f.Sync()
if err != nil {
l.RaiseError("unable to sync file: %v", err)
}
l.Push(lua.LNumber(float64(bytes)))
return 1
}