\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{cizixs (cizixs)} \pdfinfo{ /Title (go-files.pdf) /Creator (Cheatography) /Author (cizixs (cizixs)) /Subject (go files Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{BA2337} \definecolor{LightBackground}{HTML}{FAF1F2} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{go files Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{cizixs (cizixs)} via \textcolor{DarkBackground}{\uline{cheatography.com/43449/cs/12942/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}cizixs (cizixs) \\ \uline{cheatography.com/cizixs} \\ \uline{\seqsplit{cizixs}.com} \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 26th September, 2017.\\ Updated 26th September, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{2.96 cm} x{5.04 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Basic Operations}} \tn % Row 0 \SetRowColor{LightBackground} create empty file & `newFile, err := os.Create("test.txt")` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} truncate a file & `err := os.Truncate("test.txt", 100)` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} get file info & `fileInfo, err := os.State("test.txt")` \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} rename a file & `err := os.Rename(oldPath, newPath)` \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} delete a file & `err := os.Remove("test.txt")` \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} open a file for reading & `file, err := os.Open("test.txt")` \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} open a file & `file, err := os.Open("test.txt", os.O\_APPEND, 0600)` \tn % Row Count 15 (+ 3) % Row 7 \SetRowColor{white} close a file & `err := file.Close()` \tn % Row Count 16 (+ 1) % Row 8 \SetRowColor{LightBackground} change file permision & `err := os.Chmod("test.txt", 0777)` \tn % Row Count 18 (+ 2) % Row 9 \SetRowColor{white} change file ownership & `err := os.Chown("test.txt", os.Getuid(), os.Getgid())` \tn % Row Count 21 (+ 3) % Row 10 \SetRowColor{LightBackground} change file timestamps & `err := os.Chtimes("test.txt", lastAccessTime, lastModifyTime)` \tn % Row Count 24 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2 cm} x{6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{file open flag}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{`os.O\_RDONLY`} & open the file read only \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{`os.O\_WRONLY`} & open the file write only \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{`os.O\_RDWR`} & open the file read write \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{`os.O\_APPEND`} & append data to the file when writing \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{`os.O\_CREATE`} & create a new file if none exists \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{`os.O\_EXCL`} & used with `O\_CREATE`, file must not exist \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{`os.O\_SYNC`} & open for synchronous I/O \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} `O\_TRUNC` & if possible, truncate file when opened \tn % Row Count 16 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{When opening file with `os.OpenFile`, flags control how the file behaves.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.8 cm} x{5.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Hard Link \& Symbol Link}} \tn % Row 0 \SetRowColor{LightBackground} create a hard link & `err := os.Link("test.txt", "test\_copy.txt")` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} create a symbol link & `err := os.Symlink("test.txt", "test\_sym.txt")` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} get link file info & `fileInfo, err := \seqsplit{os.Lstat("test\_sym.txt")`} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} change link file owner & `err := \seqsplit{os.Lchown("test\_sym.txt"}, uid, gid)` \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} read a link & `dest, err := \seqsplit{os.ReadLink("link\_file.txt")`} \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{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. \newline \newline A symbolic link, or soft link, only reference other files by name. They can point to files on different filesystems. Not all systems support symlinks.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Read and Write}} \tn % Row 0 \SetRowColor{LightBackground} write bytes to file & `n, err := file.Write({[}{]}byte("hello, world!\textbackslash{}n"))` \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} write string to file & `n, err := file.WriteString("Hello, world!\textbackslash{}n")` \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} write at offset & `n, err := file.WriteAt({[}{]}byte("Hello"), 10)` \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} read to byte & `n, err := file.Read(byteSlice)` \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} read exactly n bytes & `n, err := io.ReadFull(file, byteSlice)` \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} read at least n bytes & `n, err := io.ReadAtLeast(file, byteSlice, minBytes)` \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} read all bytes of a file & `byteSlice, err := ioutil.ReadAll(file)` \tn % Row Count 16 (+ 2) % Row 7 \SetRowColor{white} read from offset & `n, err := file.ReadAt(byteSlice, 10)` \tn % Row Count 18 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.44 cm} x{4.56 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Work with directories}} \tn % Row 0 \SetRowColor{LightBackground} create a directory & `err := os.Mkdir("myDir", 0600)` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} recursively create a directory & `err := \seqsplit{os.MkdirAll("dir/subdir/myDir"}, 0600)` \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} delete a directory recursively & `err := os.RemoveAll("dir/")` \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} list directory files & `fileInfo, err := ioutil.ReadDir(".")` \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.64 cm} x{5.36 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Shortcuts}} \tn % Row 0 \SetRowColor{LightBackground} quick read from file & `byteSlice, err := \seqsplit{ioutil.ReadFile("test.txt")`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} quick write to file & `err := \seqsplit{ioutil.WriteFile("test.txt"}, {[}{]}byte("Hello"), 0666)` \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} copy file & `n, err := io.Copy(newFile, originFile)` \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} write string to file & `io.WriteString(file, "Hello, world")` \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.32 cm} x{5.68 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Temporary files and directories}} \tn % Row 0 \SetRowColor{LightBackground} create temp dir & `ioutil.TempDir(dir, prefix string) (name string, err error)` \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} create temp file & `ioutil.TempFile(dir, prefix string) (f *os.File, err error)` \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{References}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{link="http://devdungeon.com/content/working-files-go"\}\}Working with Files in Go\{\{/link\}\}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{link="https://golang.org/pkg/os/"\}\}golang `os` standard library\{\{/link\}\}} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{link="https://golang.org/pkg/io/ioutil"\}\}golang `ioutil` standard library\{\{/link\}\}} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{link="https://golang.org/pkg/io"\}\}golang `iou` standard library\{\{/link\}\}} \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}