builtin file operations
This commit is contained in:
26
object/builtin_read.go
Normal file
26
object/builtin_read.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package object
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// Read ...
|
||||
func Read(args ...Object) Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
|
||||
arg, ok := args[0].(*String)
|
||||
if !ok {
|
||||
return newError("argument to `read` expected to be `str` got=%T", args[0].Type())
|
||||
}
|
||||
|
||||
filename := arg.Value
|
||||
data, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return newError("error reading file: %s", err)
|
||||
}
|
||||
|
||||
return &String{Value: string(data)}
|
||||
}
|
||||
Reference in New Issue
Block a user