0% found this document useful (0 votes)
98 views2 pages

Go Files Cheat Sheet: by Via

This document provides a cheat sheet for working with files and directories in Go. It outlines basic file operations like creating, opening, reading, writing, and deleting files. It also covers creating hard and symbolic links, changing file permissions/ownership, and working with directories. The cheat sheet provides code snippets and explanations of common file and I/O functions in the Go standard library.

Uploaded by

mmm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
98 views2 pages

Go Files Cheat Sheet: by Via

This document provides a cheat sheet for working with files and directories in Go. It outlines basic file operations like creating, opening, reading, writing, and deleting files. It also covers creating hard and symbolic links, changing file permissions/ownership, and working with directories. The cheat sheet provides code snippets and explanations of common file and I/O functions in the Go standard library.

Uploaded by

mmm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 2

go files Cheat Sheet

by cizixs (cizixs) via cheatography.com/43449/cs/12942/

Basic Operations Hard Link & Symbol Link

create empty newFile, err := os.Cre​ate​("te​st.t​xt​") create a hard link err := os.Lin​k("t​est.tx​t",
file "​tes​t_c​opy.tx​t")
truncate a file err := os.Tru​nca​te(​"​tes​t.t​xt", 100)
create a symbol err := os.Sym​lin​k("t​est.tx​t",
get file info fileInfo, err := os.Sta​te(​"​tes​t.t​xt") link "​tes​t_s​ym.t​xt​")

rename a file err := os.Ren​ame​(ol​dPath, newPath) get link file info fileInfo, err :=
os.Lst​at(​"​tes​t_s​ym.t​xt​")
delete a file err := os.Rem​ove​("te​st.t​xt​")
change link file err := os.Lch​own​("te​st_​sym.tx​t", uid,
open a file for file, err := os.Ope​n("t​est.tx​t")
owner
reading gid)

open a file file, err := os.Ope​n("t​est.tx​t", read a link dest, err :=


os.O_A​PPEND, 0600) os.Rea​dLi​nk(​"​lin​k_f​ile.tx​t")

close a file err := file.C​lose() 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.Chm​od(​"​tes​t.t​xt", 0777)
same file system. A hard link is what you might consider a 'normal' link.
permision

change file err := os.Cho​wn(​"​tes​t.t​xt", os.Get​uid(),


A symbolic link, or soft link, only reference other files by name. They can
ownership os.Get​gid()) point to files on different filesy​stems. Not all systems support symlinks.

change file err := os.Cht​ime​s("t​est.tx​t",


timestamps lastAc​ces​sTime, lastMo​dif​yTime) Read and Write

write bytes to n, err := file.W​rit​e([​]by​te(​"​hello,


file open flag file world!​\n"))

os.O_​RDONLY open the file read only write string to n, err := file.W​rit​eSt​rin​g("H​ello,
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.W​rit​eAt​([]​byt​e("H​ell​o"),
10)
os.O_​APPEND append data to the file when writing
read to byte n, err := file.R​ead​(by​teS​lice)
os.O_​CREATE create a new file if none exists
read exactly n n, err := io.Rea​dFu​ll(​file, byteSl​ice)
os.O_​EXCL used with O_CRE​ATE, file must not exist
bytes
os.O_​SYNC open for synchr​onous I/O read at least n n, err := io.Rea​dAt​Lea​st(​file,
O_TRUNC if possible, truncate file when opened bytes byteSlice, minBytes)

When opening file with os.Op​enF​ile, flags control how the file behaves. read all bytes of byteS​lice, err := ioutil.Re​adA​ll(​file)
a file

read from offset n, err := file.R​ead​At(​byt​eSlice, 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 direct​ories

create a directory err := os.Mkd​ir(​"​myD​ir", 0600)

recurs​ively create a err :=


directory os.Mkd​irA​ll(​"​dir​/su​bdi​r/m​yDi​r",
0600)

delete a directory err := os.Rem​ove​All​("di​r/")


recurs​ively

list directory files fileInfo, err :=


ioutil.Re​adD​ir(​".")

Shortcuts

quick read byteS​lice, err :=


from file ioutil.Re​adF​ile​("te​st.t​xt​")

quick write to err := ioutil.Wr​ite​Fil​e("t​est.tx​t",


file []byte​("He​llo​"), 0666)

copy file n, err := io.Cop​y(n​ewFile, origin​File)

write string io.Wr​ite​Str​ing​(file, "​Hello, world")


to file

Temporary files and direct​ories

create iouti​l.T​emp​Dir​(dir, prefix string) (name


temp dir string, err error)

create iouti​l.T​emp​Fil​e(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

You might also like