\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{{[}deleted{]}} \pdfinfo{ /Title (golang-naming-conventions.pdf) /Creator (Cheatography) /Author ({[}deleted{]}) /Subject (Golang Naming Conventions 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}{2980B9} \definecolor{LightBackground}{HTML}{F1F7FA} \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{Golang Naming Conventions Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{{[}deleted{]}} via \textcolor{DarkBackground}{\uline{cheatography.com/70653/cs/17920/}}} \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}{[}deleted{]} \\ \uline{cheatography.com/deleted-70653} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 15th November, 2018.\\ Updated 15th November, 2018.\\ 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} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Why naming is important?}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Critical for Readability = Maintainability}} \newline % Row Count 1 (+ 1) The naming is important because it is very critical for readability and if you can't read the code, you can't properly maintain it.\{\{nl\}\} \newline % Row Count 4 (+ 3) {\emph{Imagine a book that you don't understand, and someone comes to you and asks you to fix the typos in it.}}\{\{nl\}\} \newline % Row Count 7 (+ 3) {\emph{Can you really do it, without understanding it?}}% Row Count 8 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{There are only two hard things in Computer Science:}} \newline {\bf{cache invalidation and naming things.}} \newline {\bf{- Phil Karlton}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Non-Idiomatic}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{func Read(buffer *Buffer, inBuffer {[}{]}byte) (size int, err error) \{ \newline if buffer.empty() \{ \newline buffer.Reset() \newline \} \newline size = copy( \newline inBuffer, \newline buffer.buffer{[}buffer.offset:{]}) \newline buffer.offset += size \newline return size, nil \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{This code is unnecessarily verbose. Everything has been declared in English words, which generally should be avoided. From the readability and maintainability perspective, this code is not good.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Idiomatic}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{func Read(b *Buffer, p {[}{]}byte) (n int, err error) \{ \newline if b.empty() \{ \newline b.Reset() \newline \} \newline n = copy(p, b.buf{[}b.off:{]}) \newline b.off += n \newline return n, nil \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{This code is very concise and idiomatic and it's easy to understand and maintain.} \tn \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}}{\{\{popup="https://github.com/BluntSporks/abbreviation"\}\}Abbreviation in Go\{\{/popup\}\}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{popup="https://golang.org/pkg/bytes/"\}\}golang `bytes` standard library\{\{/popup\}\}} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{popup="https://bit.ly/LEARNGO-CHEATSHEETS"\}\}Inanc Gumus - Learn Go Programming\{\{/popup\}\}} \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Use the first few letters of the words}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`var {\bf{fv}} string // {\bf{f}}lag {\bf{v}}alue`% Row Count 1 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Use fewer letters in smaller scopes}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`var {\bf{bytesRead}} int // number of {\bf{bytes read}}` \{\{fa-times\}\} \newline % Row Count 2 (+ 2) `var {\bf{n}} int // {\bf{n}}umber of bytes read` \{\{fa-check\}\}% Row Count 4 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Use the complete words in larger scopes}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`package {\bf{file}}` \newline % Row Count 1 (+ 1) `var {\bf{file}}Closed bool`% Row Count 2 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Imagine that this variable is declared in the package block of the ~`{\bf{file}}`~ package. \newline \newline It's a package level variable and therefore it's in a larger scope. Don't use abbreviations there and don't mix caps in the name. `{\bf{file}}`~ starts with a lowercase letter.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Use mixedCaps like this}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`type {\bf{p}}layer{\bf{S}}core struct`% Row Count 1 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Use all caps for acronyms}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`var local{\bf{Api}} string` \{\{fa-times\}\} \newline % Row Count 1 (+ 1) `var local{\bf{API}} string` \{\{fa-check\}\}% Row Count 2 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Do not stutter}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`player.{\bf{Player}}Score` \{\{fa-times\}\} \newline % Row Count 1 (+ 1) `{\bf{player}}.Score` \{\{fa-check\}\}% Row Count 2 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Do not use under\_scores oR LIKE\_THIS}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`const {\bf{MAX\_TIME}} int` \{\{fa-times\}\} \newline % Row Count 1 (+ 1) `const {\bf{MaxTime}} int` \{\{fa-check\}\} \newline % Row Count 2 (+ 1) `const {\bf{N}} int` \{\{fa-check\}\}% Row Count 3 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Abbreviation (page break)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Abbreviation - Rules}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Sound/Spelling}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Abbreviations should be {\bf{pronounceable}}.\{\{nl\}\}\{\{nl\}\} Abbreviations should have {\bf{at least one vowel}}.\{\{nl\}\}\{\{nl\}\} Abbreviations should not split up {\bf{plosive/liquid}} combinations but as {\bf{plosive/plosive}}, for example, the ~{\bf{ct}} in {\bf{dictionary}} or {\bf{pt}} in {\bf{caption}}.\{\{nl\}\}\{\{nl\}\} Abbreviations should not have more than {\bf{three consonants}} in a row and should usually {\bf{end in a consonant}}, unless the vowel is needed for discrimination, for example, {\bf{alg}} and {\bf{algo}}.\{\{nl\}\}\{\{nl\}\} All of the letters in the abbreviation should be present in the long form and in the same order, and need not appear in sequence in the long form, for example, {\bf{recv}} and {\bf{receive}}.} \tn % Row Count 16 (+ 16) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{nl\}\}{\bf{Exceptions/Limitations}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}There are a few exceptions to the above rules for common, well-established forms.\{\{nl\}\}\{\{nl\}\} {\bf{ct}} and {\bf{pt}} can be used for~{\bf{ction}} and {\bf{ption}} if the abbreviation would be too short otherwise, for example, {\bf{act}} and {\bf{opt}}.\{\{nl\}\}\{\{nl\}\} There are also other types of prefixing, for example, the three-letter prefixes used to distinguish field names in the same database table.\{\{nl\}\}\{\{nl\}\} Examples would include {\bf{cusID}} for {\bf{customer ID}} and {\bf{ordID}} for {\bf{order ID}}.\{\{nl\}\}\{\{nl\}\} Those prefixes don't need to follow the same rules.} \tn % Row Count 29 (+ 13) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{nl\}\}{\bf{Length/Meaning and Interpretation}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}An abbreviation should be less than or equal to half the length of the original form.\{\{nl\}\}\{\{nl\}\} Abbreviations should be {\bf{at least three letters long}}.\{\{nl\}\}\{\{nl\}\} Abbreviations should {\bf{not be whole words}} that mean something else.\{\{nl\}\}\{\{nl\}\} Abbreviations should not just consist of the prefix of a word, for example, {\bf{sym}} for {\bf{symbol}} or {\bf{syl}} for {\bf{syllable}}.\{\{nl\}\}\{\{nl\}\} Abbreviations {\bf{shouldn't be ambiguous}}. However, if the names are different that {\bf{no confusion}} can result, they are {\bf{OK}}.} \tn % Row Count 41 (+ 12) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Abbreviation (column break)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.68 cm} x{4.32 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Abbreviation}} \tn % Row 0 \SetRowColor{LightBackground} `var {\bf{a}} int` & // {\bf{a}}rray \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `var {\bf{arg}} {[}{]}string` & // {\bf{arg}}ument \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} `var {\bf{b}} {[}{]}byte` & // {\bf{b}}uffer \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} `var {\bf{b}} byte` & // {\bf{b}}yte \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} `var {\bf{bs}} bytes` & // {\bf{b}}yte{\bf{s}} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} `var {\bf{buf}} {[}{]}byte` & // {\bf{buf}}fer \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} `var {\bf{c}} int` & // {\bf{c}}apacity \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} `var {\bf{c}} int` & // {\bf{c}}haracter \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} `var {\bf{dst}} int` & // {\bf{d}}e{\bf{st}}ination \tn % Row Count 12 (+ 2) % Row 9 \SetRowColor{white} `var {\bf{err}} error` & // {\bf{err}}or value \tn % Row Count 14 (+ 2) % Row 10 \SetRowColor{LightBackground} `var {\bf{fv}} string` & // {\bf{f}}lag {\bf{v}}alue \tn % Row Count 16 (+ 2) % Row 11 \SetRowColor{white} `var {\bf{i}} int` & // {\bf{i}}ndex \tn % Row Count 17 (+ 1) % Row 12 \SetRowColor{LightBackground} `var {\bf{l}} int` & // {\bf{l}}ength \tn % Row Count 18 (+ 1) % Row 13 \SetRowColor{white} `var {\bf{m}} int` & // another nu{\bf{m}}ber \tn % Row Count 19 (+ 1) % Row 14 \SetRowColor{LightBackground} `var {\bf{msg}} string` & // {\bf{m}}e{\bf{s}}sa{\bf{g}}e \tn % Row Count 21 (+ 2) % Row 15 \SetRowColor{white} `var {\bf{n}} int` & // {\bf{n}}umber or number of \tn % Row Count 23 (+ 2) % Row 16 \SetRowColor{LightBackground} `var {\bf{num}} int` & // {\bf{num}}ber \tn % Row Count 24 (+ 1) % Row 17 \SetRowColor{white} `var {\bf{off}} int` & // {\bf{off}}set \tn % Row Count 25 (+ 1) % Row 18 \SetRowColor{LightBackground} `var {\bf{op}} int` & // {\bf{op}}eration \tn % Row Count 26 (+ 1) % Row 19 \SetRowColor{white} `var {\bf{parsed}} bool` & // {\bf{parsed}} ok? \tn % Row Count 28 (+ 2) % Row 20 \SetRowColor{LightBackground} `var {\bf{pkg}} string` & // {\bf{p}}ac{\bf{k}}a{\bf{g}}e \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.68 cm} x{4.32 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Abbreviation (cont)}} \tn % Row 21 \SetRowColor{LightBackground} `var {\bf{pos}} int` & // {\bf{pos}}ition \tn % Row Count 1 (+ 1) % Row 22 \SetRowColor{white} `var {\bf{r}} rune` & // {\bf{r}}une \tn % Row Count 2 (+ 1) % Row 23 \SetRowColor{LightBackground} `var {\bf{r}} io.Reader` & // {\bf{r}}eader \tn % Row Count 4 (+ 2) % Row 24 \SetRowColor{white} `var {\bf{s}} string` & // {\bf{s}}tring \tn % Row Count 5 (+ 1) % Row 25 \SetRowColor{LightBackground} `var {\bf{seen}} bool` & // has {\bf{seen}}? \tn % Row Count 7 (+ 2) % Row 26 \SetRowColor{white} `var {\bf{sep}} string` & // {\bf{sep}}arator \tn % Row Count 9 (+ 2) % Row 27 \SetRowColor{LightBackground} `var {\bf{src}} int` & // {\bf{s}}ou{\bf{rc}}e \tn % Row Count 10 (+ 1) % Row 28 \SetRowColor{white} `var {\bf{str}} string` & // {\bf{str}}ing \tn % Row Count 12 (+ 2) % Row 29 \SetRowColor{LightBackground} `var {\bf{v}} string` & // {\bf{v}}alue \tn % Row Count 13 (+ 1) % Row 30 \SetRowColor{white} `var {\bf{val}} string` & // {\bf{val}}ue \tn % Row Count 15 (+ 2) % Row 31 \SetRowColor{LightBackground} `var {\bf{w}} io.Writer` & // {\bf{w}}riter \tn % Row Count 17 (+ 2) % Row 32 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{...the list goes on and on...} \tn % Row Count 18 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}