Files
monkey/object/builtin_read.go
Chuck Smith 5574c23597
Some checks failed
Test / build (push) Waiting to run
Build / build (push) Has been cancelled
builtin file operations
2024-03-24 12:15:25 -04:00

27 lines
479 B
Go

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)}
}