en Home Smtp Sometimes I fall in love with code jgdb A Gopher named Gophy Encode/Decode Golang data to Gob files GoWatch Cfg Go Snippets Ping Citadel vCard Sync Your Everyday Go Tools Cases Conv Go jkl Encode/Decode between struct and byte slice Crypt HDKeys Wordlist HDKeys Recover

File Access and Create Time

Gets the Modified, Create and Access time of a file

Little update (like 5 years later).
Just tried this on Windows and no it doesn't work.
I get following error:

`undefined: syscall.Stat_t`

This works on Linux.
Haven't tried on any other platform, please report if you do.

https://play.golang.org/p/vJFZ1o_qIp

package main

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

type FileTime struct {
    MTime time.Time
    CTime time.Time
    ATime time.Time
}

const FILE = "/opt/go/LICENSE"

func main(){

    file, err := FTime(FILE)
    if err == nil {
        fmt.Println("Mo Time", file.MTime)
        fmt.Println("Ac Time", file.ATime)
        fmt.Println("Cr Time", file.CTime)
    }
}

// Gets the Modified, Create and Access time of a file
func FTime(file string) (t *FileTime, err error) {
    fileinfo, err := os.Stat(file)
    if err != nil {
        return
    }
    t = new(FileTime)
    var stat = fileinfo.Sys().(*syscall.Stat_t)
    t.ATime = time.Unix(stat.Atim.Sec, stat.Atim.Nsec)
    t.CTime = time.Unix(stat.Ctim.Sec, stat.Ctim.Nsec)
    t.MTime = time.Unix(stat.Mtim.Sec, stat.Mtim.Nsec)
    return
}

First Created: 2015-04-25T14:02:07Z
Last Modified: 2026-04-05 19:35