Standard library / io/fs
fs.ReadFile()
Reads the full named file and closes it when using the fallback Open path. Successful EOF is not an error; consider size limits. Expected stdout: 'ok <nil>\n'.
Formal Syntax & Structure
go syntaxgo
func ReadFile(fsys FS, name string) ([]byte, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| fsys | FS | Filesystem implementing fs.FS. |
| name | string | Slash-separated filesystem path. |
Return Value
([]byte, error)
Reads the full named file and closes it when using the fallback Open path.
Working Example
Example Snippetgo
package main
import ( "fmt" "io/fs" "testing/fstest" "time")
func tree() fstest.MapFS { return fstest.MapFS{"a.txt": &fstest.MapFile{Data: []byte("ok"), Mode: 0644, ModTime: time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC)}, "sub/b.txt": &fstest.MapFile{Data: []byte("go"), Mode: 0644}}}func main() { b, e := fs.ReadFile(tree(), "a.txt"); fmt.Println(string(b), e) }