Last active
December 21, 2015 15:49
-
-
Save kelseyhightower/6329156 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"syscall" | |
"time" | |
) | |
func main() { | |
f, err := os.Open("test.txt") | |
defer f.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
fi, err := f.Stat() | |
if err != nil { | |
log.Fatal(err) | |
} | |
// The follow statements will print the following output on a linux box. | |
// Basic: | |
// name: test.txt | |
// size: 11 | |
// mode: -rw-r--r-- | |
// modified: 2013-08-24 09:36:36.234671631 -0700 PDT | |
fmt.Print("Basic:\n") | |
fmt.Printf(" name: %s\n size: %d\n mode: %s\n modified: %s\n", | |
fi.Name(), fi.Size(), fi.Mode(), fi.ModTime()) | |
// More details using the syscall package | |
var s syscall.Stat_t | |
// Get the file descriptor of test.txt | |
fd := int(f.Fd()) | |
err = syscall.Fstat(fd, &s) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Produce a human readable timestamp when printed as a string. | |
// Note: s.Atim, s.Mtim, and s.Ctim are not defined on OS X. | |
atime := time.Unix(s.Atim.Unix()) | |
mtime := time.Unix(s.Mtim.Unix()) | |
ctime := time.Unix(s.Ctim.Unix()) | |
// The follow statements will print the following output on a linux box. | |
// Advanced: | |
// inode: 395365 | |
// links: 1 | |
// uid: 1000 | |
// gid: 1000 | |
// size: 11 | |
// blockSize: 4096 | |
// blocks: 8 | |
// atime: 2013-08-24 09:12:57.172472315 -0700 PDT | |
// mtime: 2013-08-24 09:36:36.234671631 -0700 PDT | |
// ctime: 2013-08-24 09:36:36.234671631 -0700 PDT | |
fmt.Print("\nAdvanced:\n") | |
fmt.Printf(" inode: %d\n links: %d\n uid: %d\n gid: %d\n"+ | |
" size: %d\n blockSize: %d\n blocks: %d\n"+ | |
" atime: %s\n mtime: %s\n ctime: %s\n", | |
s.Ino, s.Nlink, s.Uid, s.Gid, | |
s.Size, s.Blksize, s.Blocks, | |
atime, mtime, ctime) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment