\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.pdf) /Creator (Cheatography) /Author ({[}deleted{]}) /Subject (Golang 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}{A7C1E8} \definecolor{LightBackground}{HTML}{F4F7FC} \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 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{{[}deleted{]}} via \textcolor{DarkBackground}{\uline{cheatography.com/23330/cs/5127/}}} \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-23330} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 7th September, 2015.\\ Updated 12th May, 2016.\\ 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*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Basic Syntax}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{package main \newline import "fmt" \newline func main() \{ \newline fmt.Println("Hello Go") \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Packages}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{- package declaration at top of every source file \newline % Row Count 1 (+ 1) - executables are in package main \newline % Row Count 2 (+ 1) - convention: package name == last name of import path (import path math/rand =\textgreater{} package rand) \newline % Row Count 4 (+ 2) - upper case identifier: exported (visible from other packages) \newline % Row Count 6 (+ 2) - Lower case identifier: private (not visible from other packages)% Row Count 8 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Operators}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Arithmetic}} \newline + addition \newline - subtraction \newline {\emph{ multiplication \newline / quotient \newline \% remainder \newline \& bitwise and \newline | bitwise or \newline \textasciicircum{} bitwise xor \newline \&\textasciicircum{} bit clear (and not) \newline \textless{}\textless{} left shift \newline \textgreater{}\textgreater{} right shift \newline {\bf{Comparison}} \newline == equal \newline != not equal \newline \textless{} less than \newline \textless{}= less than or equal \newline \textgreater{} greater than \newline \textgreater{}= greater than or equal \newline {\bf{Logical}} \newline \&\& logical and \newline || logical or \newline ! logical not \newline {\bf{Other}} \newline \& address of / create pointer \newline }} dereference pointer \newline \textless{}- send / receive operator} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// a simple function \newline func functionName() \{\} \newline // function with parameters \newline func functionName(param1 string, param2 int) \{\} \newline // multiple parameters of the same type \newline func functionName(param1, param2 int) \{\} \newline // return type declaration \newline func functionName() int \{ \newline return 42 \newline \} \newline // return multiple \newline func returnMulti() (int, string) \{ \newline return 42, "foobar" \newline \} \newline var x, str = returnMulti() \newline // Return multiple named results simply by return \newline func returnMulti2() (n int, s string) \{ \newline n = 42 \newline s = "foobar" \newline // n and s will be returned \newline return \newline \} \newline var x, str = returnMulti2() \newline {\bf{Functions As Values And Closures}} \newline func main() \{ \newline // assign a function to a name \newline add := func(a, b int) int \{ \newline return a + b \newline \} \newline // use the name to call the function \newline fmt.Println(add(3, 4)) \newline \} \newline // Closures, lexically scoped: Functions can access values that were \newline // in scope when defining the function \newline func scope() func() int\{ \newline outer\_var := 2 \newline foo := func() int \{ return outer\_var\} \newline return foo \newline \} \newline func another\_scope() func() int\{ \newline // won't compile because outer\_var and foo not defined in this scope \newline outer\_var = 444 \newline return foo \newline \} \newline // Closures: don't mutate outer vars, instead redefine them! \newline func outer() (func() int, int) \{ \newline outer\_var := 2 \newline inner := func() int \{ \newline outer\_var += 99 // attempt to mutate outer\_var from outer scope \newline return outer\_var // =\textgreater{} 101 (but outer\_var is a newly redefined \newline // variable visible only inside inner) \newline \} \newline return inner, outer\_var // =\textgreater{} 101, 2 (outer\_var is still 2, not mutated by foo!) \newline \} \newline {\bf{Variadic Functions}} \newline func main() \{ \newline fmt.Println(adder(1, 2, 3)) // 6 \newline fmt.Println(adder(9, 9)) // 18 \newline \newline nums := {[}{]}int\{10, 20, 30\} \newline \seqsplit{fmt.Println(adder(nums...))} // 60 \newline \} \newline \newline // By using ... before the type name of the last parameter you can indicate that it takes zero or more of those parameters. \newline // The function is invoked like any other function except we can pass as many arguments as we want. \newline func adder(args ...int) int \{ \newline total := 0 \newline for \_, v := range args \{ // Iterates over the arguments whatever the number. \newline total += v \newline \} \newline return total \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Declarations}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{var foo int // declaration without initial. \newline var foo int = 42 // declaration with initial \newline var foo, bar int = 42, 1302 // declare and init \newline var foo = 42 // type omitted, will be inferred \newline foo := 42 // shorthand \newline const constant = "This is a constant"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Type Conversions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{var i int = 42 \newline var f float64 = float64(i) \newline var u uint = uint(f) \newline \newline // alternative syntax \newline i := 42 \newline f := float64(i) \newline u := uint(f)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Arrays, Slices, Ranges}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Arrays}} \newline var a {[}10{]}int \newline // declare an int array with length 10. Array length is part of the type! \newline a{[}3{]} = 42 // set elements \newline i := a{[}3{]} // read elements \newline // declare and initialize \newline var a = {[}2{]}int\{1, 2\} \newline a := {[}2{]}int\{1, 2\} //shorthand \newline a := {[}...{]}int\{1, 2\} // elipsis -\textgreater{} Compiler figures out array length \newline \newline {\bf{Slices}} \newline var a {[}{]}int // declare a slice - similar to an array, but length is unspecified \newline var a = {[}{]}int \{1, 2, 3, 4\} // declare and initialize a slice (backed by the array given implicitly) \newline a := {[}{]}int\{1, 2, 3, 4\} // shorthand \newline chars := {[}{]}string\{0:"a", 2:"c", 1: "b"\} // {[}"a", "b", "c"{]} \newline var b = a{[}lo:hi{]} // creates a slice (view of the array) from index lo to hi-1 \newline var b = a{[}1:4{]} // slice from index 1 to 3 \newline var b = a{[}:3{]} // missing low index implies 0 \newline var b = a{[}3:{]} // missing high index implies len(a) \newline // create a slice with make \newline a = make({[}{]}byte, 5, 5) // first arg length, second capacity \newline a = make({[}{]}byte, 5) // capacity is optional \newline // create a slice from an array \newline x := {[}3{]}string\{"Лайка", "Белка", "Стрелка"\} \newline s := x{[}:{]} // a slice referencing the storage of x} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Built-in Types}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{bool \newline string \newline int int8 int16 int32 int64 \newline uint uint8 uint16 uint32 uint64 uintptr \newline byte // alias for uint8 \newline rune // alias for int32 \textasciitilde{}= a character (Unicode code point) - very Viking \newline float32 float64 \newline complex64 complex128} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Control structures}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{If}} \newline func main() \{ \newline // Basic one \newline if x \textgreater{} 0 \{ \newline return x \newline \} else \{ \newline return -x \newline \} \newline // You can put one statement before the condition \newline if a := b + c; a \textless{} 42 \{ \newline return a \newline \} else \{ \newline return a - 42 \newline \} \newline // Type assertion inside if \newline var val interface\{\} \newline val = "foo" \newline if str, ok := val.(string); ok \{ \newline fmt.Println(str) \newline \} \newline \} \newline {\bf{Loops}} \newline // There only for, no while, no until \newline for i := 1; i \textless{} 10; i++ \{ \newline \} \newline for ; i \textless{} 10; \{ // while - loop \newline \} \newline for i \textless{} 10 \{ // omit semicolons \newline \} \newline for \{ //omit the condition \textasciitilde{} while (true) \newline \} \newline {\bf{Switch}} \newline switch operatingSystem \{ \newline case "darwin": \newline fmt.Println("Mac OS Hipster") \newline // cases break automatically \newline case "linux": \newline fmt.Println("Linux Geek") \newline default: \newline // Windows, BSD, ... \newline fmt.Println("Other") \newline \} \newline \newline // as with for and if, you can have an assignment statement before the switch value \newline switch os := runtime.GOOS; os \{ \newline case "darwin": ... \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}