\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{Andan H M (andanhm)} \pdfinfo{ /Title (golang.pdf) /Creator (Cheatography) /Author (Andan H M (andanhm)) /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}{2096F3} \definecolor{LightBackground}{HTML}{F1F8FE} \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}{Andan H M (andanhm)} via \textcolor{DarkBackground}{\uline{cheatography.com/69306/cs/17474/}}} \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}Andan H M (andanhm) \\ \uline{cheatography.com/andanhm} \\ \uline{\seqsplit{andanhm}.me} \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 21st October, 2018.\\ Updated 22nd October, 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}{Go}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Go (also referred to as GoLang) is an open source and lower level programming language designed and created at Google in 2009 by Robert Griesemer, Rob Pike and Ken Thompson, to enable users to easily write simple, reliable, and highly efficient computer programs} \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Besides its better-known aspects such as built-in concurrency and garbage collection} \tn % Row Count 8 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Go is a statically typed language, it is anti functional programming and anti OOP, as far as the designers concerned.} \tn % Row Count 11 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{https://golang.org/} \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}{Feature}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Language is very concise, simple and safe.} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Compilation time is very fast.} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Patterns which adapt to the surrounding environment similar to dynamic languages.} \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Inbuilt concurrency such as lightweight processes channels and select statements.} \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Supports the interfaces and the embedded types.} \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{https://golang.org/doc/faq}} \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}{Lack of essential features}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{No ternary operator ?:} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{No generic types} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{No exceptions} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{No assertions} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{No overloading of methods and operators} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\textasciitilde{}\textasciitilde{}GOPATH is a mess\textasciitilde{}\textasciitilde{}} \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\textasciitilde{}\textasciitilde{}Package dependence manage tool\textasciitilde{}\textasciitilde{}} \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{https://github.com/ksimka/go-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}{Companies Using Golang}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Google for "dozens of systems"} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Docker a set of tools for deploying linux containers} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Openshift a cloud computing platform as a service by Red Hat} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Dropbox migrated few of their critical components from Python to Go} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Netflix for two portions of their server architecture} \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Soundcloud for "dozens of systems"} \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{ThoughtWorks some tools and applications around continuous delivery and instant messages (CoyIM)} \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Uber for handling high volumes of geofence-based queries.} \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{BookMyShow for handling high volume of traffic, rapidly growing customer, to adapt new business solution and (cloud solution) distribution tools} \tn % Row Count 17 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{https://www.qwentic.com/blog/companies-using-golang}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{1.36 cm} x{6.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Install}} \tn % Row 0 \SetRowColor{LightBackground} OSX & brew install go \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Run the command below to view your Go version: \newline `go version` \newline \newline https://golang.org/doc/install} \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}{Directory layout}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{GOPATH=/home/user/go} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{/home/user/go/ \newline ~~src/ \newline ~~~~hello/ \newline ~~~~~~main.go (package main) \newline ~~bin/ \newline ~~~~ hello (installed command) \newline ~~pkg/ \newline ~~~~linux\_amd64/ (installed package object) \newline ~~~~github.com/ (3rd party dependencies)} \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}{Hello Word}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{package main \newline \newline import "fmt" \newline \newline func main() \{ \newline fmt.Println("Hello, World!!") \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Create a file named main.go in the directory src/hello inside your workspace/go path \newline {\bf{go env}} Default go system environment \newline \newline https://tour.golang.org} \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}{Running}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\$ cd \$HOME/go/src/hello \newline \$ go run main.go \newline Hello, World!! \newline \$ go build \newline \$ ./hello \newline Hello, World!!} \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}{Package}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Package declaration at top of every source file} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Executables are should be in package main} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Upper case identifier: public (accessible from other packages)} \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Lower case identifier: private (not accessible from other packages)} \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}{Built-in Types}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{bool \newline \newline string \newline \newline int int8 int16 int32 int64 \newline uint uint8 uint16 uint32 uint64 uintptr \newline \newline byte // alias for uint8 \newline \newline rune // alias for int32 \textasciitilde{}= a character (Unicode code point) \newline \newline float32 float64 \newline \newline complex64 complex128} \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}{Packages and Modules}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Packages}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Go packages are folders that contain one more go files.} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Modules}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A modules (starting with vgo and go 1.11) is a versioned collection of packages.} \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`go get \seqsplit{github.com/andanhm/go-prettytimee`} \newline `go mod init \seqsplit{github.com/andanhm/go-prettytime`}} \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}{Variable \& Function Declarations}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{const country = "india" \newline \newline // declaration without initialization \newline var age int \newline // declaration with initialization \newline var age int = 23 \newline // declare and init multiple at once \newline var age, pincode int = 23, 577002 \newline // type omitted, will be inferred \newline var age = 23 \newline \newline // simple function \newline func person() \{ \newline // shorthand, only within func bodies \newline // type is always implicit \newline age := 23 \newline \} \newline \newline // Can have function with params \newline func person(firstName string, lastName string) \{\} \newline \newline // Can have multiple params of the same type \newline func person(firstName, lastName string) \{\} \newline \newline // Can return type declaration \newline func person() int \{ \newline return 23 \newline \} \newline \newline // Can return multiple values at once \newline func person() (int, string) \{ \newline return 23, "vinay" \newline \} \newline var age, name = person() \newline \newline // Can return multiple named results \newline func person() (age int, name string) \{ \newline age = 23 \newline name = "vinay" \newline return \newline \} \newline var age, name = person() \newline \newline // Can return function \newline func person() func() (string,string) \{ \newline area:=func() (string,string) \{ \newline return "street", "city" \newline \} \newline return area \newline \}} \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}{If statement}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{if age \textless{} 18 \{ \newline return errors.New("not allowed to enter") \newline \} \newline \newline // Conditional statement \newline if err := Request("google.com"); err != nil \{ \newline return err \newline \} \newline \newline // Type assertion inside \newline var age interface\{\} \newline age = 23 \newline if val, ok := age.(int); ok \{ \newline fmt.Println(val) \newline \}} \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}{Loop statement}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{for i := 1; i \textless{} 3; i++ \{ \newline \} \newline // while loop syntax \newline for i \textless{} 3 \{ \newline \} \newline // Can omit semicolons if there is only a condition \newline for i \textless{} 10 \{ \newline \} \newline // while (true) like syntax \newline for \{ \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Go don't have {\bf{while}} {\bf{until}}} \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}{Switch statement}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{// switch statement \newline switch runtime.GOOS \{ \newline case "darwin": \{ \newline // cases break automatically \newline \} \newline case "linux": \{ \newline \} \newline default: \newline \} \newline \newline // can have an assignment statement before the switch statement \newline switch os := runtime.GOOS; os \{ \newline case "darwin": \newline default: \newline \} \newline \newline // comparisons in switch cases \newline os := runtime.GOOS \newline switch \{ \newline case os == "darwin": \newline default: \newline \} \newline \newline // cases can be presented in comma-separated lists \newline switch os \{ \newline case "darwin", "linux": \newline \}} \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}{Arrays, Slices}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var a {[}3{]}int // declare an int array with length 3. \newline var a = {[}3{]}int \{1, 2, 3\} // declare and initialize a slice \newline a := {[}...{]}int\{1, 2\} // elipsis -\textgreater{} Compiler figures out array length \newline a{[}0{]} = 1 // set elements \newline i := a{[}0{]} // read elements \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 a = append(a,17,3) // append items to slice a \newline c := append(a,b...) // concatenate slices a and b \newline \newline // create a slice with make \newline a = make({[}{]}int, 5, 5) // first arg length, second capacity \newline a = make({[}{]}int, 5) // capacity is optional \newline \newline // loop over an array/ slice / struct \newline for index, element := range a \{ \newline \newline \}} \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}{Maps \& Struct}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Maps}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Maps are Go's built-in associative data type (`hashes` or `dicts`)} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Struct}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Structs are the way to create concrete user-defined types in Go. Struct types are declared by composing a fixed set of unique fields.} \tn % Row Count 7 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{type Address struct \{ \newline Street string \newline City string \newline \} \newline \newline type Employee struct \{ \newline Name string \newline Age int \newline Address Address \newline \} \newline \newline // Can declare methods on structs. \newline func (emp Employee) Display() string \{ \newline // accessing member \newline name:=emp.Name \newline return fmt.Sprintf("Name \%s",name) \newline \} \newline // Initialize the map with the type \newline // map key is city value employees working \newline bookmyshow := make(map{[}string{]}{[}{]}Employee) \newline \newline // Create new/updates the key value pair \newline bookmyshow{[}"Pune"{]} = {[}{]}Employee\{\} \newline bookmyshow{[}"Bangalore"{]} = {[}{]}Employee\{ \newline Employee\{ \newline Name: "Andan H M", \newline Age: 23, \newline Address: Address\{ \newline Street: "KB Extension", \newline City: "Davanagere", \newline \}, \newline \}, \newline \}, \newline \newline // Determains the the length of the map \newline \_ = len(bookmyshow) \newline \newline // read the item from the map \newline employees := bookmyshow{[}"Bangalore"{]} \newline \newline // loop over an array, slice, struct array \newline for index, element := range employees \{ \newline // read the element from the struct \newline fmt.Println(index, element.Display()) \newline \} \newline \newline // Delete the key from the map \newline delete(bookmyshow, "Pune")} \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}{Interfaces}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Interface type that specifies zero methods is known as the {\emph{empty interface}} \newline \newline var i interface\{\} \newline i = 42 \newline \newline // Reflection: {\emph{type}} switch specify types \newline switch v := i.(type) \{ \newline case int: \newline fmt.Printf("(\%v, \%T)\textbackslash{}n", i, i) \newline case string: \newline fmt.Printf("(\%v, \%T)\textbackslash{}n", i, i) \newline default: \newline fmt.Printf("Unknow type \%T!\textbackslash{}n", v) \newline \} \newline \newline Interfaces are named collections of method signatures. \newline \newline type error interface \{ \newline Error() string \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Accept interfaces, return structs}}} \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}{Error}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{The error type is an interface type.} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`error` variable represents description of the error string} \tn % Row Count 3 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`errors.New('user not found')` \newline `fmt.Errorf("\%s user not found", "foo")` \newline https://blog.golang.org/error-handling-and-go} \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}{HTTP Handler}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{package main \newline \newline import ( \newline "io" \newline "net/http" \newline ) \newline \newline func health(w http.ResponseWriter, r *http.Request) \{ \newline \seqsplit{w.WriteHeader(http.StatusOK)} \newline io.WriteString(w, "Ok") \newline \} \newline \newline func main() \{ \newline \seqsplit{http.HandleFunc("/health"}, health) \newline \seqsplit{http.ListenAndServe(":8080"}, nil) \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{A \seqsplit{mini-toolkit/micro-framework} to build web apps; with handler chaining, middleware and context injection, with standard library compliant HTTP handlers(i.e. http.HandlerFunc). \newline \newline https://github.com/bnkamalesh/webgo} \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}{Unit Test}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Go has a built-in testing command called go test and a package testing which combine to give a minimal but complete testing experience.} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Standard tool-chain also includes benchmarking and code coverage} \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{https://github.com/andanhm/gounittest}} \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}{Concurrency}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Goroutines}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Goroutines are lightweight threads managed by Go} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Channels}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Channels are a typed conduit through which you can send and receive values with the channel operator ( `\textless{}-` )} \tn % Row Count 7 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{package main \newline \newline import "fmt" \newline \newline func main() \{ \newline n := 2 \newline \newline // "make" the channel, which can be used \newline // to move the int datatype \newline out := make(chan int) \newline \newline // run this function as a goroutine \newline // the channel that we made is also provided \newline go Square(n, out) \newline \newline // Any output is received on this channel \newline // print it to the console and proceed \newline fmt.Println(\textless{}-out) \newline \} \newline \newline func Square(n int, out chan\textless{}- int) \{ \newline result := n * n \newline \newline //pipes the result into it \newline out \textless{}- result \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\emph{select}} statement lets a goroutine wait on multiple communication operations. \newline \newline {\emph{sync}} go build-in package provides basic synchronization primitives such as mutual exclusion locks. \newline https://golang.org/pkg/sync/} \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}{Defer, Panic, and Recover}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Defer}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A defer statement pushes a function call onto a Last In First Out order list. The list of saved calls is executed after the surrounding function returns} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Panic}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Panic is a built-in function that stops the ordinary flow of control and begins panicking.} \tn % Row Count 8 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Recover}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Recover is a built-in function that regains control of a panicking goroutine} \tn % Row Count 11 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`func main() \{` \newline `~defer func() \{` \newline `~if r := recover(); r != nil \{` \newline ` ~~ fmt.Println("Recovered", r)` \newline `~ \}` \newline ` ~\}()` \newline `~panic("make panic")` \newline `\}`} \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}{Encoding}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\emph{encoding}} is a built-in package defines interfaces shared by other packages that convert data to and from byte-level and textual representations} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Go offers built-in support for encoding/gob, encoding/json, and encoding/xml} \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{https://golang.org/pkg/encoding/}} \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}{Example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{package main \newline \newline import ( \newline "encoding/json" \newline "encoding/xml" \newline "fmt" \newline ) \newline \newline type Employee struct \{ \newline Name string \textbackslash{}`json:"name" xml:"name"\textbackslash{}` \newline Age int \textbackslash{}`json:"age" xml:"age"\textbackslash{}` \newline \} \newline \newline func main() \{ \newline emp := Employee\{ \newline Name: "andan.h", \newline Age: 27, \newline \} \newline // Marshal: refers to the process of converting \newline // the data or the objects into a byte-stream \newline jsonData, \_ := json.Marshal(emp) \newline \seqsplit{fmt.Println(string(jsonData))} \newline xmlData, \_ := xml.Marshal(emp) \newline \seqsplit{fmt.Println(string(xmlData))} \newline \newline // Unmarshal: refers to the reverse process of \newline // converting the byte-stream back to data or object \newline json.Unmarshal(jsonData, \&emp) \newline fmt.Println(emp) \newline \}} \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}{Tool}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{https://godoc.org/golang.org/x/tools}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{https://dominik.honnef.co/posts/2014/12/an\_incomplete\_list\_of\_go\_tools/}} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{https://github.com/campoy/go-tooling-workshop}} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}