# miekg/gobook

### Subversion checkout URL

You can clone with
or
.
 7811b5c Fix all the quotes and more [from: Stefan Schroeder] miekg authored Mar 10, 2012 1 \epi{I am interested in this and hope to do something.''} 1f704ac sorta fix small caps miekg authored Oct 12, 2010 2 {\textit{On adding complex numbers to Go}\\ \textsc{KEN THOMPSON}} 30324e7 added google logo miekg authored May 11, 2010 3 f4a1ee3 add a small preface miekg authored Aug 13, 2011 4 \noindent{}What is Go? From the website \cite{go_web}: 7ae1471 final phase for 0.2 miekg authored Oct 17, 2010 5 \begin{quote} 6 The Go programming language is an open source project to make 7 programmers more productive. Go is expressive, concise, clean, and 8 efficient. Its concurrency mechanisms make it easy to write programs 5c25b47 Spell check miekg authored Aug 18, 2011 9 that get the most out of multi core and networked machines, while its 7ae1471 final phase for 0.2 miekg authored Oct 17, 2010 10 novel type system enables flexible and modular program construction. Go 11 compiles quickly to machine code yet has the convenience of garbage 12 collection and the power of run-time reflection. It's a fast, statically 13 typed, compiled language that feels like a dynamically typed, 14 interpreted language. 15 \end{quote} 3dd4e3f change the introduction a bit (order) miekg authored Jun 30, 2010 16 c41e6c1 Tell about Go1 miekg authored Apr 8, 2012 17 Go1 is the first stable release of the language Go. 18 This document and all exercises work with Go1 -- if not, its 19 a bug. 7ae1471 final phase for 0.2 miekg authored Oct 17, 2010 20 21 The following convention is used throughout this book: 164f7a7 start new repo miekg authored May 9, 2010 22 \begin{itemize} c5c6e34 Correct font in graphics (dejavu) miekg authored Aug 9, 2010 23 \item Code is displayed in \prog{DejaVu Mono}; 24 \item Keywords are displayed in \key{DejaVu Mono Bold}; 25 \item Comments are displayed in \rem{DejaVu Mono Italic}; aa4f15b add more stuff about defer miekg authored Jul 19, 2010 26 \item Extra remarks in the code \coderemark{Are displayed like this}; 24a8471 small style changes miekg authored Oct 12, 2010 27 \item Longer remarks get a number -- \gocircle{1} -- with the explanation following; 57068d9 style fixes miekg authored Oct 15, 2010 28 \item Line numbers are printed on the right side; c80be2c Use letters for footnotes miekg authored Aug 21, 2010 29 \item Shell examples use a \pr{} as prompt; bc1bf00 some minor changes miekg authored Aug 16, 2011 30 \item User entered text in shell examples \texttt{\user{is in bold}}, system responses 31 are in a \texttt{typewriter font}; 278d4d4 spell check miekg authored Oct 22, 2010 32 \item An emphasized paragraph is indented and has a vertical bar on the c80be2c Use letters for footnotes miekg authored Aug 21, 2010 33 left. 164f7a7 start new repo miekg authored May 9, 2010 34 \end{itemize} 35 36 \section{Official documentation} fb87c1c JCs comments for chapter 1 miekg authored Aug 12, 2010 37 There already is a substantial amount of documentation written about Go. 7811b5c Fix all the quotes and more [from: Stefan Schroeder] miekg authored Mar 10, 2012 38 \gomarginpar{When searching on the internet use the term golang'' instead of plain go''.} fb87c1c JCs comments for chapter 1 miekg authored Aug 11, 2010 39 The Go Tutorial \cite{go_tutorial}, and the Effective Go 164f7a7 start new repo miekg authored May 9, 2010 40 document \cite{effective_go}. The fb87c1c JCs comments for chapter 1 miekg authored Aug 11, 2010 41 website \url{http://golang.org/doc/} is a very good starting point 8966fda more proof reading fixes miekg authored Aug 16, 2010 42 for reading up on Go\footnote{\url{http://golang.org/doc/} itself is served by 7811b5c Fix all the quotes and more [from: Stefan Schroeder] miekg authored Mar 10, 2012 43 a Go program called \prog{go\ doc}.}. Reading these documents is 353124c extend the todo miekg authored Aug 21, 2010 44 certainly not required, but is recommended. 164f7a7 start new repo miekg authored May 9, 2010 45 7811b5c Fix all the quotes and more [from: Stefan Schroeder] miekg authored Mar 10, 2012 46 Go comes with its own documentation in the form of a program called f01b58c Use go doc instead of godoc miekg authored Feb 7, 2012 47 \prog{go doc}. 7ae1471 final phase for 0.2 miekg authored Oct 17, 2010 48 You can use it yourself to look 5c25b47 Spell check miekg authored Aug 18, 2011 49 in the on-line documentation. For 7ae1471 final phase for 0.2 miekg authored Oct 17, 2010 50 instance, suppose we want to know more about the package \package{hash}. f01b58c Use go doc instead of godoc miekg authored Feb 7, 2012 51 We would then give the command \prog{go doc hash}. eb27712 remove another bunch of todos miekg authored Jan 31, 2011 52 How to create your own package documentation 7ae1471 final phase for 0.2 miekg authored Oct 17, 2010 53 is explained in chapter \ref{chap:packages}. 54 3ab1e13 Update the intro chapter miekg authored Feb 6, 2012 55 \section{Origins} 56 Go has it origins in Inferno \cite{inferno} (which in turn was based 57 upon Plan 9 \cite{plan9}). Inferno included a language called Limbo 58 \cite{limbo}. Quoting from the Limbo paper: 59 \begin{quote} 60 Limbo is a programming language intended for applications running 61 distributed systems on small computers. It supports modular programming, 62 strong type checking at compile- and run-time, \emph{inter process 63 communication over typed channels}, automatic \emph{garbage collection}, and 64 simple abstract data types. It is designed for safe execution even on 65 small machines without hardware memory protection. 66 \end{quote} 67 A feature Go inherited from Limbo is channels (see chapter 68 \ref{chap:channels}). Again from the Limbo documentation. 69 \begin{quote} 70 [A channel] is a communication mechanism capable of sending and receiving objects of 71 the specified type to another agent in the system. Channels may be used 72 to communicate between local processes; using library procedures, they 73 may be connected to named destinations. In either case send and receive 74 operations may be directed to them. 75 \end{quote} 76 The channels in Go are easier to use than those in Limbo. 77 If we dig even deeper in the history of Go we also find references 7811b5c Fix all the quotes and more [from: Stefan Schroeder] miekg authored Mar 10, 2012 78 to Newsqueak'' \cite{newsqueak}, which pioneered the use of 3ab1e13 Update the intro chapter miekg authored Feb 6, 2012 79 channel communication in a C--like language. Channel 80 communication isn't unique to these languages, a big non--C--like 81 language which also uses them is Erlang \cite{erlang}. 82 83 \begin{figure}[H] 84 \caption{Chronology of Go} 85 \label{fig:chrono-of-go} 86 \begin{center} 87 \includegraphics[scale=0.65]{fig/go-history.pdf} 88 \end{center} 89 \end{figure} 90 91 The whole of idea of using channels to communicate with other processes 92 is called Communicating Sequential Processes (CSP) and was conceived 93 by C. A. R. Hoare \cite{hoare}, who incidentally is the same man that 94 invented QuickSort \cite{quicksort}. 95 96 \begin{lbar}[] 97 Go is the first C--like language that is widely available, 98 runs on many 99 different platforms and makes concurrency easy (or easier). 100 \end{lbar} 101 0f58f2a First set of improvements miekg authored Oct 19, 2010 102 \section{Getting Go} 3ab1e13 Update the intro chapter miekg authored Feb 6, 2012 103 In this section we tell how to install Go locally on your machine, but you can 104 also compile Go code online at \url{http://play.golang.org/}. To quickly 105 play with code this is by far the easiest route. 106 cb09cad add a note about packages that exist miekg authored Jul 31, 2011 107 Ubuntu and Debian both have a Go package in their repositories, look for 7811b5c Fix all the quotes and more [from: Stefan Schroeder] miekg authored Mar 10, 2012 108 the package golang''. But there are still some minor issues being worked cb09cad add a note about packages that exist miekg authored Jul 31, 2011 109 out. For now we will stick to the installation from source. 110 111 So we will retrieve the code from the mercurial archive and compile 0f58f2a First set of improvements miekg authored Oct 19, 2010 112 Go yourself. For other Unix like systems the procedure is the same. 164f7a7 start new repo miekg authored May 9, 2010 113 \begin{itemize} fb87c1c JCs comments for chapter 1 miekg authored Aug 11, 2010 114 \item First install Mercurial (to get the \prog{hg} command). In 164f7a7 start new repo miekg authored May 9, 2010 115 Ubuntu/Debian/Fedora you must install the \prog{mercurial} package; 116 1e848fe more tweaks to the first 2 chapters miekg authored Feb 1, 2011 117 \item For building Go you need the packages: \prog{bison}, 164f7a7 start new repo miekg authored May 9, 2010 118 \prog{gcc}, \prog{libc6-dev}, \prog{ed}, \prog{gawk} and \prog{make}; 119 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 120 \item Set the environment variable \prog{GOROOT} to the root of your 121 Go install: 122 \begin{display} 961db26 Allow for ~ miekg authored Mar 10, 2012 123 \pr \user{export GOROOT=/go} 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 124 \end{display} 125 164f7a7 start new repo miekg authored May 9, 2010 126 \item Then retrieve the Go source code: e454eba better spacing miekg authored Jun 1, 2010 127 \begin{display} 091ca7a Some shell examples updates miekg authored Jul 28, 2011 128 \pr \user{hg clone -r release https://go.googlecode.com/hg/ $GOROOT} e454eba better spacing miekg authored Jun 1, 2010 129 \end{display} b26ce31 clear up the first chapter miekg authored Jun 25, 2010 130 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 131 \item Set your PATH to so that the Shell can find the Go binaries: b26ce31 clear up the first chapter miekg authored Jun 25, 2010 132 \begin{display} 091ca7a Some shell examples updates miekg authored Jul 28, 2011 133 \pr \user{export PATH=$GOROOT/bin:$PATH} b26ce31 clear up the first chapter miekg authored Jun 25, 2010 134 \end{display} 164f7a7 start new repo miekg authored May 9, 2010 135 \item Compile Go e454eba better spacing miekg authored Jun 1, 2010 136 \begin{display} 091ca7a Some shell examples updates miekg authored Jul 28, 2011 137 \pr \user{cd$GOROOT/src} 138 \pr \user{./all.bash} e454eba better spacing miekg authored Jun 1, 2010 139 \end{display} 164f7a7 start new repo miekg authored May 9, 2010 140 \end{itemize} b4d7f06 Fix use of \user in display's miekg authored Feb 10, 2012 141 If all goes well, you should see the following at the end: 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 142 \begin{display} b4d7f06 Fix use of \user in display's miekg authored Feb 10, 2012 143 --- cd ../test 144 0 known bugs; 0 unexpected bugs 145 146 ALL TESTS PASSED 147 148 --- 149 Installed Go for linux/amd64 in /home/go 150 Installed commands in /home/go/bin 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 151 \end{display} 152 You now have Go installed on your system and you can start playing. 6390061 More talk about the versions miekg authored Mar 19, 2012 153 Note that currently (March 2012) this install version r60 of, this version 154 is old. If you want Go1, or something close to it, you will have to d3b9832 quoting stuff mikespook authored Mar 20, 2012 155 upgrade to the latest weekly, see the section \titleref{sec:weekly}''. 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 156 c8f8b29 add the Windows text from Paulo Pinto miekg authored Sep 26, 2011 157 \section{Getting Go for Windows} 0637de0 Some more updates from Paulo miekg authored Oct 2, 2011 158 The best way to use Go in Windows is together with the MinGW \cite{go_windows} environment. 159 It provides an UNIX like environment for Windows. It is similar to Cygwin, but no POSIX 160 emulation is attempted, the Microsoft C run time library and Windows APIs are used instead. c8f8b29 add the Windows text from Paulo Pinto miekg authored Sep 26, 2011 161 \begin{itemize} 162 \item Download the latest version from \cite{go_windows}; 163 \item Unpack it to your \verb|C:\| drive; 0637de0 Some more updates from Paulo miekg authored Oct 2, 2011 164 \item Make sure that the contents are \verb|C:\MinGW|. Note: this directory should be 165 created when you unpacked MinGW; c8f8b29 add the Windows text from Paulo Pinto miekg authored Sep 26, 2011 166 \item Use the mintty shortcut to start a terminal window; 167 \item Set the environment variable \prog{GOROOT} to the root of your 168 Go install: 169 \begin{display} 170 \pr \user{export GOROOT=/c/go} 171 \end{display} 172 \item Download the latest Go stable release by issuing the following command 173 \begin{display} 174 \pr \user{hg clone -r release https://go.googlecode.com/hg/ $GOROOT} 175 \end{display} 176 \item Change to the \file{src} directory 177 \begin{display} 178 \pr \user{cd$GOROOT/src} 179 \end{display} 180 \item And build Go 181 \begin{display} 182 \pr \user{./all.bash} 183 \end{display} 184 \end{itemize} 185 You should now have a working Go environment. Next we need to configure b115c53 Small tweaks miekg authored Feb 7, 2012 186 the GOBIN and PATH variables. Note that the GOROOT variable is also included here. c8f8b29 add the Windows text from Paulo Pinto miekg authored Sep 26, 2011 187 188 \begin{itemize} 189 \item Create a file named \file{setup.sh} and place it inside the \file{MinGW} directory 190 with the following content: 191 \begin{display} b115c53 Small tweaks miekg authored Feb 7, 2012 192 export GOROOT=/c/go c8f8b29 add the Windows text from Paulo Pinto miekg authored Sep 26, 2011 193 export GOBIN=$GOROOT/bin 194 export PATH=$GOBIN:$PATH 195 \end{display} 196 \item Now when you start mintty, you can source the file to setup your Go enviroment: 197 \begin{display} 198 \pr \user{. ./setup.sh} 199 \end{display} 200 \end{itemize} 201 202 \section{Keeping up to date} 6390061 More talk about the versions miekg authored Mar 19, 2012 203 \label{sec:weekly} 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 204 New releases are announced on the Go Nuts mailing list \cite{go_nuts}. To update an 205 existing tree to the latest release, you can run: 206 \begin{display} 091ca7a Some shell examples updates miekg authored Jul 28, 2011 207 \pr \user{cd$GOROOT} 208 \pr \user{hg pull} 209 \pr \user{hg update release} 210 \pr \user{cd src} 211 \pr \user{./all.bash} 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 212 \end{display} d05e417 remove stuff miekg authored Feb 3, 2011 213 \noindent{}To see what you are running right now: 0f213da Fixes from the code checker miekg authored Oct 24, 2010 214 \begin{display} 091ca7a Some shell examples updates miekg authored Jul 28, 2011 215 \pr \user{cd \$GOROOT} 216 \pr \user{hg identify} 0f213da Fixes from the code checker miekg authored Oct 24, 2010 217 79997f0e5823 release/release.2010-10-20 218 \end{display} bc1bf00 some minor changes miekg authored Aug 16, 2011 219 \noindent{}That would be release \gorelease{2010-10-20}. The release as 7811b5c Fix all the quotes and more [from: Stefan Schroeder] miekg authored Mar 10, 2012 220 describe is a stable'' releases, 221 as opposed to the weekly'' releases that are more volatile. bc1bf00 some minor changes miekg authored Aug 16, 2011 222 If you want to track the weekly releases 2102ba7 more typos miekg authored Aug 13, 2011 223 instead of the stable ones you can use: 224 \begin{display} 225 \pr \user{hg update weekly} 226 \end{display} 227 In stead of 228 \begin{display} 229 \pr \user{hg update release} 230 \end{display} 37db19b start section on gofix miekg authored Jun 27, 2011 231 164f7a7 start new repo miekg authored May 9, 2010 232 \section{Exercises} 16a6b17 add another question miekg authored May 14, 2010 233 \input{ex-intro/ex-doc.tex} 234 164f7a7 start new repo miekg authored May 9, 2010 235 \cleardoublepage 236 \section{Answers} 237 \shipoutAnswer