Show Menu

go files Cheat Sheet by

working with files in golang

Basic Operations

create empty file
newFile, err := os.Cre­ate­("te­st.t­xt­")
truncate a file
err := os.Tru­nca­te(­"­tes­t.t­xt", 100)
get file info
fileInfo, err := os.Sta­te(­"­tes­t.t­xt")
rename a file
err := os.Ren­ame­(ol­dPath, newPath)
delete a file
err := os.Rem­ove­("te­st.t­xt­")
open a file for reading
file, err := os.Ope­n("t­est.tx­t")
open a file
file, err := os.Ope­n("t­est.tx­t", os.O_A­PPEND, 0600)
close a file
err := file.C­lose()
change file permision
err := os.Chm­od(­"­tes­t.t­xt", 0777)
change file ownership
err := os.Cho­wn(­"­tes­t.t­xt", os.Get­uid(), os.Get­gid())
change file timestamps
err := os.Cht­ime­s("t­est.tx­t", lastAc­ces­sTime, lastMo­dif­yTime)

file open flag

open the file read only
open the file write only
open the file read write
append data to the file when writing
create a new file if none exists
used with O_CRE­ATE, file must not exist
open for synchr­onous I/O
if possible, truncate file when opened
When opening file with os.Op­enF­ile, flags control how the file behaves.

Hard Link & Symbol Link

create a hard link
err := os.Lin­k("t­est.tx­t", "­tes­t_c­opy.tx­t")
create a symbol link
err := os.Sym­lin­k("t­est.tx­t", "­tes­t_s­ym.t­xt­")
get link file info
fileInfo, err := os.Lst­at(­"­tes­t_s­ym.t­xt­")
change link file owner
err := os.Lch­own­("te­st_­sym.tx­t", uid, gid)
read a link
dest, err := os.Rea­dLi­nk(­"­lin­k_f­ile.tx­t")
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 same file system. A hard link is what you might consider a 'normal' link.

A symbolic link, or soft link, only reference other files by name. They can point to files on different filesy­stems. Not all systems support symlinks.

Read and Write

write bytes to file
n, err := file.W­rit­e([­]by­te(­"­hello, world!­\n"))
write string to file
n, err := file.W­rit­eSt­rin­g("H­ello, world!­\n")
write at offset
n, err := file.W­rit­eAt­([]­byt­e("H­ell­o"), 10)
read to byte
n, err := file.R­ead­(by­teS­lice)
read exactly n bytes
n, err := io.Rea­dFu­ll(­file, byteSl­ice)
read at least n bytes
n, err := io.Rea­dAt­Lea­st(­file, byteSlice, minBytes)
read all bytes of a file
byteS­lice, err := ioutil.Re­adA­ll(­file)
read from offset
n, err := file.R­ead­At(­byt­eSlice, 10)

Work with direct­ories

create a directory
err := os.Mkd­ir(­"­myD­ir", 0600)
recurs­ively create a directory
err := os.Mkd­irA­ll(­"­dir­/su­bdi­r/m­yDi­r", 0600)
delete a directory recurs­ively
err := os.Rem­ove­All­("di­r/")
list directory files
fileInfo, err := ioutil.Re­adD­ir(­".")


quick read from file
byteS­lice, err := ioutil.Re­adF­ile­("te­st.t­xt­")
quick write to file
err := ioutil.Wr­ite­Fil­e("t­est.tx­t", []byte­("He­llo­"), 0666)
copy file
n, err := io.Cop­y(n­ewFile, origin­File)
write string to file
io.Wr­ite­Str­ing­(file, "­Hello, world")

Temporary files and direct­ories

create temp dir
iouti­l.T­emp­Dir­(dir, prefix string) (name string, err error)
create temp file
iouti­l.T­emp­Fil­e(dir, prefix string) (f *os.File, err error)

Download the go files Cheat Sheet

2 Pages

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!



No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          GoLang fmt Printing Cheat Sheet