Standard library / os

os.Stat()

Returns metadata for a named file, following a symbolic link to its target. Example uses a disposable temporary workspace. Expected stdout: '3 true\n'.

Formal Syntax & Structure

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

Parameters & Attributes

NameTypeDescription
namestringName or pathname of the target.

Return Value

(FileInfo, error)

Returns metadata for a named file, following a symbolic link to its target.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os")
func check(e error) {  if e != nil {    panic(e)  }}func seed() { check(os.WriteFile("a.txt", []byte("abc"), 0600)) }func workspace() func() {  root, e := os.MkdirTemp("", "bytemind-go-")  check(e)  old, e := os.Getwd()  check(e)  check(os.Chdir(root))  return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() {  defer workspace()()  seed()  i, e := os.Stat("a.txt")  check(e)  fmt.Println(i.Size(), i.Mode().IsRegular())}
Go Reference