Go Files Cheat Sheet: by Via
Go Files Cheat Sheet: by Via
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)
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
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
Shortcuts
References