Can I get the last access time from a file?

How I can get the last access date of a file?

From what I see in the " fileinfo " structure does not exist when this property .

Thanks in advance!

Hey there!

Here is how I get the last access time for a specific file on my specific operating system.

Edit: Since this is using system calls, it’s best to use a package such as https://github.com/djherbis/atime for cross system compatibility.

package main

import (
	"fmt"
	"os"
	"syscall"
	"time"
)

func main() {
	// Get an `os.FileInfo` object for the file `hello.txt`.
	fi, err := os.Stat("hello.txt")
	if err != nil {
		return
	}

	// Get the last access time for `hello.txt`.
	accessTime := fi.Sys().(*syscall.Stat_t).Atimespec

	// Print out the last access time.
	fmt.Println(time.Unix(accessTime.Sec, accessTime.Nano()))
}
1 Like

Hi Benjamin!

Thankyou very much!

Regards,
Claudio.-

1 Like

Be aware that it’s common for Linux systems to switch off support for atime values at filesystem level, to improve performance. For example, Fedora sets filesystems to noatime by default.

1 Like

seems to not work with noatime. so, can be a problem on ssd drives for example.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.