Standard library / os

os.SameFile()

Reports whether two FileInfo values from Stat describe the same underlying file, using OS-specific identity rules. Example uses a disposable temporary workspace. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func SameFile(fi1, fi2 FileInfo) bool

Parameters & Attributes

NameTypeDescription
fi1FileInfoFirst file metadata.
fi2FileInfoSecond file metadata.

Return Value

bool

Reports whether two FileInfo values from Stat describe the same underlying file, using OS-specific identity rules.

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()  a, e := os.Stat("a.txt")  check(e)  b, e := os.Stat("a.txt")  check(e)  fmt.Println(os.SameFile(a, b))}
Go Reference