Standard library / io/fs

fs.Stat()

Returns metadata for a named path, dispatching to StatFS when supported or opening and closing the file. Expected stdout: 'a.txt 2 <nil>\n'.

Formal Syntax & Structure

go syntaxgo
func Stat(fsys FS, name string) (FileInfo, error)

Parameters & Attributes

NameTypeDescription
fsysFSFilesystem implementing fs.FS.
namestringSlash-separated filesystem path.

Return Value

(FileInfo, error)

Returns metadata for a named path, dispatching to StatFS when supported or opening and closing the file.

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() { i, e := fs.Stat(tree(), "a.txt"); fmt.Println(i.Name(), i.Size(), e) }
Go Reference