\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{Navron} \pdfinfo{ /Title (go-lang.pdf) /Creator (Cheatography) /Author (Navron) /Subject (Go Lang 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}{A850EB} \definecolor{LightBackground}{HTML}{F4E9FC} \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 Lang Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Navron} via \textcolor{DarkBackground}{\uline{cheatography.com/185051/cs/38662/}}} \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}Navron \\ \uline{cheatography.com/navron} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 12th May, 2023.\\ 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}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{packages}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{package main} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\emph{-Every package file has to start with package.}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.78712 cm} x{2.18988 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Importing}} \tn % Row 0 \SetRowColor{LightBackground} import "fmt" & import ( \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} import "math/rand" & "fmt" \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} & "math/rand" ) \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\emph{-Both are the same}}} \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 Loops}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{// Normal For}} \newline for i := 0; i \textless{} 10; i++ \{ \newline sum += i \newline \} \newline \newline {\bf{// For loop is Also While Loop in Go}} \newline i := 0 \newline for i \textless{} 5 \{ \newline fmt.Println(i) \newline \} \newline \newline {\bf{// For with Range}} \newline nums := {[}{]}int\{2,4,6,8\} \newline for index,val := range nums \{ \newline fmt.Println(index, val) \newline \} \newline \newline {\emph{//Here range will give the index of nums to var index}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{- No paranthesis required around conditionals and increments \newline - Variable Initialization and Increment is optional here} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Maps}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{// Creating Map with Make}} & {\bf{// creating Map}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} var name = make({[}type{]}type) & var name = map{[}type{]}type\{val:val, val:val\} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{var m = make({[}string{]}, string)} \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 6 (+ 0) % Row 4 \SetRowColor{LightBackground} {\bf{Insert or update an element in map }} & {\bf{Retrieve an element:}} \tn % Row Count 9 (+ 3) % Row 5 \SetRowColor{white} m{[}key{]} = elem & elem = m{[}key{]} \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 10 (+ 0) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Delete an element:}}} \tn % Row Count 11 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{delete(m, key)} \tn % Row Count 12 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.14011 cm} x{2.83689 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Variable Types}} \tn % Row 0 \SetRowColor{LightBackground} bool & string \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} int int8 int16 int32 int64 & uint uint8 uint16 uint32 uint64 uintptr \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} byte // alias for uint8 & rune // alias for int32 \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} float32 float64 & complex64 complex128 \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Variable Conversion}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{x := 8 \newline y := float32(x)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{you can replace float32 with different types} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.09034 cm} x{2.88666 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Array}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{// Array Declaration Only}} & {\bf{// Array Declaration without var}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} var name {[}size{]}type & name := {[}size{]}type\{val,val,val\} //size is optional \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} var a {[}10{]}string & primes := {[}6{]}int\{2, 3, 5, 7, 11, 13\} \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.44333 cm} x{3.53367 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Pointers}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{ Initailize }} & {\bf{The \& operator generates a pointer to its operand}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} var p *int & p = \&i \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 3 (+ 0) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{The * operator denotes the pointer's underlying value.}}} \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{*p = 21} \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{If-else \& Switch Case}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{ If-Else}} & {\bf{ if-else with Short Statement}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} if conditional \{ code \} & if short statement; conditional \{code\} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} else \{ code \} & if v := math.Pow(x, n); v \textless{} lim \{ \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} & return v \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} & \} else \{ \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} & fmt.Printf("\%g \textgreater{}= \%g\textbackslash{}n", v, lim) \} \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 10 (+ 0) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{ Switch Case}}} \tn % Row Count 11 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{switch conditional} \tn % Row Count 12 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{case 0:} \tn % Row Count 13 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{code} \tn % Row Count 14 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{case 1:} \tn % Row Count 15 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{code} \tn % Row Count 16 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{default:} \tn % Row Count 17 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\emph{-Variables declared inside an if short statement are only in scope until the end of if-else block}} \newline {\emph{-Go's switch cases need not be constants, and the values involved need not be integers}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.33919 cm} x{2.63781 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Variables basic}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{// var name type = value}} & {\bf{// Declaration without type}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} var x int = 5 & x := 5 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 3 (+ 0) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{// Constant Declaration with const keyword}}} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{const Phi = 1.618} \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{":=" is called Short Assignment statement \newline Outside of function ":=" is not allowed \newline \newline Exported names :- To use any variable outside of package it must start with Capital Letter or else it will not run} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.09034 cm} x{2.88666 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Slices}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{// Slice Declaration}} & {\bf{// Creating Slice with Make}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} a{[}low : high{]} & var := make({[} {]}type, size) \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} a{[}1:4{]} & a := make({[} {]}int, 5) // len(a)=5 \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 6 (+ 0) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{// Appending Item}}} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{name = append(name, val)} \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{a = append(a, "Something")} \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{- An array has a fixed size. A slice, on the other hand, is a dynamically-sized, flexible view into the elements of an array \newline - A slice does not store any data, it just describes a section of an underlying array} \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}}{' func function\_name(args type) (output types) \{ \newline CODE \newline \}' \newline \newline func swap(x, y string) (string, string) \{ \newline return y, x \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{- func is used to declaration function \newline - in case of different input type we have to specify each one differently \newline - Output type is must if there is single return then no need to add paranthesis} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Structs}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{initailize}} & {\bf{ Struct fields are accessed using a dot}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} type Name struct \{ & v := Vertex\{1, 2\} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} var type \} & v.X = 4 \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{type Vertex struct \{ X int \}} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{v := Vertex\{1, 2\}} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 7 (+ 0) % Row 6 \SetRowColor{LightBackground} {\bf{Pointers to structs}} & {\bf{Struct Literals}} \tn % Row Count 9 (+ 2) % Row 7 \SetRowColor{white} v := Vertex\{1, 2\} & var ( \tn % Row Count 11 (+ 2) % Row 8 \SetRowColor{LightBackground} p := \&v & v1 = Vertex\{1, 2\} // has type Vertex \tn % Row Count 13 (+ 2) % Row 9 \SetRowColor{white} \seqsplit{fmt.Println(p.X)} & v2 = Vertex\{X: 1\} // Y:0 is implicit \tn % Row Count 15 (+ 2) % Row 10 \SetRowColor{LightBackground} & v3 = Vertex\{\} // X:0 and Y:0 \tn % Row Count 17 (+ 2) % Row 11 \SetRowColor{white} & p = \&Vertex\{1, 2\} // has type *Vertex ) \tn % Row Count 19 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\emph{-A struct is a collection of fields}} \newline {\emph{-A struct literal denotes a newly allocated struct value by listing the values of its fields}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}