go files Cheat Sheet
by cizixs (cizixs) via cheatography.com/43449/cs/12942/
Basic Operations Hard Link & Symbol Link
create empty newFile, err := os.Create("test.txt") create a hard link err := os.Link("test.txt",
file "test_copy.txt")
truncate a file err := os.Truncate("test.txt", 100)
create a symbol err := os.Symlink("test.txt",
get file info fileInfo, err := os.State("test.txt") link "test_sym.txt")
rename a file err := os.Rename(oldPath, newPath) get link file info fileInfo, err :=
os.Lstat("test_sym.txt")
delete a file err := os.Remove("test.txt")
change link file err := os.Lchown("test_sym.txt", uid,
open a file for file, err := os.Open("test.txt")
owner
reading gid)
open a file file, err := os.Open("test.txt", read a link dest, err :=
os.O_APPEND, 0600) os.ReadLink("link_file.txt")
close a file err := file.Close() A hard link creates a new pointer to the same place. A file will only be
deleted from disk after all links are removed. Hard links only work on the
change file err := os.Chmod("test.txt", 0777)
same file system. A hard link is what you might consider a 'normal' link.
permision
change file err := os.Chown("test.txt", os.Getuid(),
A symbolic link, or soft link, only reference other files by name. They can
ownership os.Getgid()) point to files on different filesystems. Not all systems support symlinks.
change file err := os.Chtimes("test.txt",
timestamps lastAccessTime, lastModifyTime) Read and Write
write bytes to n, err := file.Write([]byte("hello,
file open flag file world!\n"))
os.O_RDONLY open the file read only write string to n, err := file.WriteString("Hello,
file world!\n")
os.O_WRONLY open the file write only
os.O_RDWR open the file read write write at offset n, err := file.WriteAt([]byte("Hello"),
10)
os.O_APPEND append data to the file when writing
read to byte n, err := file.Read(byteSlice)
os.O_CREATE create a new file if none exists
read exactly n n, err := io.ReadFull(file, byteSlice)
os.O_EXCL used with O_CREATE, file must not exist
bytes
os.O_SYNC open for synchronous I/O read at least n n, err := io.ReadAtLeast(file,
O_TRUNC if possible, truncate file when opened bytes byteSlice, minBytes)
When opening file with os.OpenFile, flags control how the file behaves. read all bytes of byteSlice, err := ioutil.ReadAll(file)
a file
read from offset n, err := file.ReadAt(byteSlice, 10)
By cizixs (cizixs) Published 26th September, 2017. Sponsored by CrosswordCheats.com
cheatography.com/cizixs/ Last updated 26th September, 2017. Learn to solve cryptic crosswords!
cizixs.com Page 1 of 2. http://crosswordcheats.com
go files Cheat Sheet
by cizixs (cizixs) via cheatography.com/43449/cs/12942/
Work with directories
create a directory err := os.Mkdir("myDir", 0600)
recursively create a err :=
directory os.MkdirAll("dir/subdir/myDir",
0600)
delete a directory err := os.RemoveAll("dir/")
recursively
list directory files fileInfo, err :=
ioutil.ReadDir(".")
Shortcuts
quick read byteSlice, err :=
from file ioutil.ReadFile("test.txt")
quick write to err := ioutil.WriteFile("test.txt",
file []byte("Hello"), 0666)
copy file n, err := io.Copy(newFile, originFile)
write string io.WriteString(file, "Hello, world")
to file
Temporary files and directories
create ioutil.TempDir(dir, prefix string) (name
temp dir string, err error)
create ioutil.TempFile(dir, prefix string) (f
temp file *os.File, err error)
References
Working with Files in Go
golang os standard library
golang ioutil standard library
golang iou standard library
By cizixs (cizixs) Published 26th September, 2017. Sponsored by CrosswordCheats.com
cheatography.com/cizixs/ Last updated 26th September, 2017. Learn to solve cryptic crosswords!
cizixs.com Page 2 of 2. http://crosswordcheats.com