# miekg/gobook

### Subversion checkout URL

You can clone with
or
.
 7225623 added quotes in the epis miekg authored Jul 9, 2011 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 c80be2c Use letters for footnotes miekg authored Aug 21, 2010 17 Go is a young language, where 18 features are still being added or even \emph{removed}. It 3dd4e3f change the introduction a bit (order) miekg authored Jun 30, 2010 19 may be possible that some text is outdated when you c80be2c Use letters for footnotes miekg authored Aug 21, 2010 20 read it. 21 Some exercise answers may become incorrect as Go continues 22 to evolve. 23 We will do our best to keep this document up to 3dd4e3f change the introduction a bit (order) miekg authored Jun 30, 2010 24 date with respect to the latest Go release. b26ce31 clear up the first chapter miekg authored Jun 25, 2010 25 An effort has been made to create "future proof" code examples. 7ae1471 final phase for 0.2 miekg authored Oct 17, 2010 26 27 The following convention is used throughout this book: 164f7a7 start new repo miekg authored May 9, 2010 28 \begin{itemize} c5c6e34 Correct font in graphics (dejavu) miekg authored Aug 9, 2010 29 \item Code is displayed in \prog{DejaVu Mono}; 30 \item Keywords are displayed in \key{DejaVu Mono Bold}; 31 \item Comments are displayed in \rem{DejaVu Mono Italic}; aa4f15b add more stuff about defer miekg authored Jul 19, 2010 32 \item Extra remarks in the code \coderemark{Are displayed like this}; 24a8471 small style changes miekg authored Oct 12, 2010 33 \item Longer remarks get a number -- \gocircle{1} -- with the explanation following; 57068d9 style fixes miekg authored Oct 15, 2010 34 \item Line numbers are printed on the right side; c80be2c Use letters for footnotes miekg authored Aug 21, 2010 35 \item Shell examples use a \pr{} as prompt; bc1bf00 some minor changes miekg authored Aug 16, 2011 36 \item User entered text in shell examples \texttt{\user{is in bold}}, system responses 37 are in a \texttt{typewriter font}; 278d4d4 spell check miekg authored Oct 22, 2010 38 \item An emphasized paragraph is indented and has a vertical bar on the c80be2c Use letters for footnotes miekg authored Aug 21, 2010 39 left. 164f7a7 start new repo miekg authored May 9, 2010 40 \end{itemize} 41 42 \section{Official documentation} fb87c1c JCs comments for chapter 1 miekg authored Aug 12, 2010 43 There already is a substantial amount of documentation written about Go. c729bfa indentation tweaks miekg authored Jul 30, 2011 44 \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 45 The Go Tutorial \cite{go_tutorial}, and the Effective Go 164f7a7 start new repo miekg authored May 9, 2010 46 document \cite{effective_go}. The fb87c1c JCs comments for chapter 1 miekg authored Aug 11, 2010 47 website \url{http://golang.org/doc/} is a very good starting point 8966fda more proof reading fixes miekg authored Aug 16, 2010 48 for reading up on Go\footnote{\url{http://golang.org/doc/} itself is served by 353124c extend the todo miekg authored Aug 21, 2010 49 a Go program called \prog{godoc}.}. Reading these documents is 50 certainly not required, but is recommended. 164f7a7 start new repo miekg authored May 9, 2010 51 7ae1471 final phase for 0.2 miekg authored Oct 17, 2010 52 Go comes with its own documentation in the form of a Go program called 53 \prog{godoc}. 54 You can use it yourself to look 5c25b47 Spell check miekg authored Aug 18, 2011 55 in the on-line documentation. For 7ae1471 final phase for 0.2 miekg authored Oct 17, 2010 56 instance, suppose we want to know more about the package \package{hash}. 57 We would then give the command \prog{godoc hash}. eb27712 remove another bunch of todos miekg authored Jan 31, 2011 58 How to create your own package documentation 7ae1471 final phase for 0.2 miekg authored Oct 17, 2010 59 is explained in chapter \ref{chap:packages}. 60 0f58f2a First set of improvements miekg authored Oct 19, 2010 61 \section{Getting Go} cb09cad add a note about packages that exist miekg authored Jul 31, 2011 62 Ubuntu and Debian both have a Go package in their repositories, look for 63 the package "golang". But there are still some minor issues being worked 64 out. For now we will stick to the installation from source. 65 66 So we will retrieve the code from the mercurial archive and compile 0f58f2a First set of improvements miekg authored Oct 19, 2010 67 Go yourself. For other Unix like systems the procedure is the same. 164f7a7 start new repo miekg authored May 9, 2010 68 \begin{itemize} fb87c1c JCs comments for chapter 1 miekg authored Aug 11, 2010 69 \item First install Mercurial (to get the \prog{hg} command). In 164f7a7 start new repo miekg authored May 9, 2010 70 Ubuntu/Debian/Fedora you must install the \prog{mercurial} package; 71 1e848fe more tweaks to the first 2 chapters miekg authored Feb 1, 2011 72 \item For building Go you need the packages: \prog{bison}, 164f7a7 start new repo miekg authored May 9, 2010 73 \prog{gcc}, \prog{libc6-dev}, \prog{ed}, \prog{gawk} and \prog{make}; 74 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 75 \item Set the environment variable \prog{GOROOT} to the root of your 76 Go install: 77 \begin{display} 091ca7a Some shell examples updates miekg authored Jul 28, 2011 78 \pr \user{export GOROOT=~/go} 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 79 \end{display} 80 164f7a7 start new repo miekg authored May 9, 2010 81 \item Then retrieve the Go source code: e454eba better spacing miekg authored Jun 1, 2010 82 \begin{display} 091ca7a Some shell examples updates miekg authored Jul 28, 2011 83 \pr \user{hg clone -r release https://go.googlecode.com/hg/ $GOROOT} e454eba better spacing miekg authored Jun 1, 2010 84 \end{display} b26ce31 clear up the first chapter miekg authored Jun 25, 2010 85 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 86 \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 87 \begin{display} 091ca7a Some shell examples updates miekg authored Jul 28, 2011 88 \pr \user{export PATH=$GOROOT/bin:$PATH} b26ce31 clear up the first chapter miekg authored Jun 25, 2010 89 \end{display} 164f7a7 start new repo miekg authored May 9, 2010 90 91 \item Compile Go e454eba better spacing miekg authored Jun 1, 2010 92 \begin{display} 091ca7a Some shell examples updates miekg authored Jul 28, 2011 93 \pr \user{cd$GOROOT/src} 94 \pr \user{./all.bash} e454eba better spacing miekg authored Jun 1, 2010 95 \end{display} 164f7a7 start new repo miekg authored May 9, 2010 96 \end{itemize} 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 97 If all goes well, you should see the following: 98 \begin{display} 091ca7a Some shell examples updates miekg authored Jul 28, 2011 99 Installed Go for linux/amd64 in /home/go. 100 Installed commands in /home/go/bin. 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 101 The compiler is 6g. 102 \end{display} 103 You now have Go installed on your system and you can start playing. 104 c8f8b29 add the Windows text from Paulo Pinto miekg authored Sep 26, 2011 105 \section{Getting Go for Windows} 106 Getting Go for Windows works slightly different, but is not difficult. 107 \begin{itemize} 108 \item Download the latest version from \cite{go_windows}; 109 \item Unpack it to your \verb|C:\| drive; 110 \item Make sure that the contents are \verb|C:\MinGW| 111 \item Use the mintty shortcut to start a terminal window; 112 \item Set the environment variable \prog{GOROOT} to the root of your 113 Go install: 114 \begin{display} 115 \pr \user{export GOROOT=/c/go} 116 \end{display} 117 \item Download the latest Go stable release by issuing the following command 118 \begin{display} 119 \pr \user{hg clone -r release https://go.googlecode.com/hg/ $GOROOT} 120 \end{display} 121 \item Change to the \file{src} directory 122 \begin{display} 123 \pr \user{cd$GOROOT/src} 124 \end{display} 125 \item And build Go 126 \begin{display} 127 \pr \user{./all.bash} 128 \end{display} 129 \end{itemize} 130 You should now have a working Go environment. Next we need to configure 131 the GOROOT, GOBIN and PATH variables. 132 133 \begin{itemize} 134 \item Create a file named \file{setup.sh} and place it inside the \file{MinGW} directory 135 with the following content: 136 \begin{display} 137 export GOROOT=/c/GOROOT 138 export GOBIN=$GOROOT/bin 139 export PATH=$GOBIN:$PATH 140 \end{display} 141 \item Now when you start mintty, you can source the file to setup your Go enviroment: 142 \begin{display} 143 \pr \user{. ./setup.sh} 144 \end{display} 145 \end{itemize} 146 147 \section{Keeping up to date} 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 148 New releases are announced on the Go Nuts mailing list \cite{go_nuts}. To update an 149 existing tree to the latest release, you can run: 150 \begin{display} 091ca7a Some shell examples updates miekg authored Jul 28, 2011 151 \pr \user{cd$GOROOT} 152 \pr \user{hg pull} 153 \pr \user{hg update release} 154 \pr \user{cd src} 155 \pr \user{./all.bash} 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 156 \end{display} d05e417 remove stuff miekg authored Feb 3, 2011 157 \noindent{}To see what you are running right now: 0f213da Fixes from the code checker miekg authored Oct 24, 2010 158 \begin{display} 091ca7a Some shell examples updates miekg authored Jul 28, 2011 159 \pr \user{cd \$GOROOT} 160 \pr \user{hg identify} 0f213da Fixes from the code checker miekg authored Oct 24, 2010 161 79997f0e5823 release/release.2010-10-20 162 \end{display} bc1bf00 some minor changes miekg authored Aug 16, 2011 163 \noindent{}That would be release \gorelease{2010-10-20}. The release as 164 describe is a "stable" releases, 165 as opposed to the "weekly" releases that are more volatile. 166 If you want to track the weekly releases 2102ba7 more typos miekg authored Aug 13, 2011 167 instead of the stable ones you can use: 168 \begin{display} 169 \pr \user{hg update weekly} 170 \end{display} 171 In stead of 172 \begin{display} 173 \pr \user{hg update release} 174 \end{display} 37db19b start section on gofix miekg authored Jun 27, 2011 175 26ca6e0 move origin section miekg authored Jun 19, 2010 176 \section{Origins} 245d0e5 I stand corrected miekg authored Nov 10, 2010 177 Go has it origins in Inferno \cite{inferno} (which in turn was based 178 upon Plan 9 \cite{plan9}). Inferno included a language called Limbo b26ce31 clear up the first chapter miekg authored Jun 25, 2010 179 \cite{limbo}. Quoting from the Limbo paper: a4c7402 check it miekg authored Jun 23, 2010 180 \begin{quote} 181 Limbo is a programming language intended for applications running 182 distributed systems on small computers. It supports modular programming, 691d301 put map exercise in the function chapter miekg authored Aug 11, 2010 183 strong type checking at compile- and run-time, \emph{inter process 3a340cb 98% finished for the first 2 chaps miekg authored Jul 1, 2010 184 communication over typed channels}, automatic \emph{garbage collection}, and a4c7402 check it miekg authored Jun 22, 2010 185 simple abstract data types. It is designed for safe execution even on 186 small machines without hardware memory protection. 187 \end{quote} 1e848fe more tweaks to the first 2 chapters miekg authored Feb 1, 2011 188 One feature of Limbo that is 189 included in Go is the support for cross compiling. b26ce31 clear up the first chapter miekg authored Jun 25, 2010 190 Another feature Go inherited from Limbo is channels (see chapter 9d694e4 more fixes for the intro chapter miekg authored Oct 17, 2010 191 \ref{chap:channels}). Again from the Limbo documentation. 2eccb75 add stuff about gos history miekg authored Jun 23, 2010 192 \begin{quote} 66c2efe two typos miekg authored Aug 15, 2010 193 [A channel] is a communication mechanism capable of sending and receiving objects of 2eccb75 add stuff about gos history miekg authored Jun 22, 2010 194 the specified type to another agent in the system. Channels may be used 195 to communicate between local processes; using library procedures, they 196 may be connected to named destinations. In either case send and receive 197 operations may be directed to them. 198 \end{quote} b26ce31 clear up the first chapter miekg authored Jun 25, 2010 199 The channels in Go are easier to use than those in Limbo. 200 If we dig even deeper in the history of Go we also find references 201 to "Newsqueak" \cite{newsqueak}, which pioneered the use of 202 channel communication in a C--like language. Channel 203 communication isn't unique to these languages, a big non--C--like 204 language which also uses them is Erlang \cite{erlang}. 205 95b6c9c add chrono fig for Go miekg authored Jun 26, 2010 206 \begin{figure}[H] 66920e2 drop the dots at the end of the captions miekg authored Aug 8, 2010 207 \caption{Chronology of Go} 95b6c9c add chrono fig for Go miekg authored Jun 26, 2010 208 \label{fig:chrono-of-go} 209 \begin{center} 210 \includegraphics[scale=0.65]{fig/go-history.pdf} 211 \end{center} 212 \end{figure} 213 b26ce31 clear up the first chapter miekg authored Jun 25, 2010 214 The whole of idea of using channels to communicate with other processes 215 is called Communicating Sequential Processes (CSP) and was conceived fb87c1c JCs comments for chapter 1 miekg authored Aug 11, 2010 216 by C. A. R. Hoare \cite{hoare}, who incidentally is the same man that 5400c2f Fixes in chapter 3 miekg authored Aug 16, 2011 217 invented QuickSort \cite{quicksort}. a4c7402 check it miekg authored Jun 22, 2010 218 a6f96cd spacing stuff miekg authored Dec 5, 2010 219 \begin{lbar}[] f800954 more on variadac args miekg authored Jul 25, 2010 220 Go is the first C--like language that is widely available, 3a340cb 98% finished for the first 2 chaps miekg authored Jul 1, 2010 221 runs on many b26ce31 clear up the first chapter miekg authored Jun 25, 2010 222 different platforms and makes concurrency easy (or easier). 223 \end{lbar} 7bb76f6 Give credit to the people how are helping miekg authored Jun 19, 2010 224 164f7a7 start new repo miekg authored May 9, 2010 225 \section{Exercises} 16a6b17 add another question miekg authored May 14, 2010 226 \input{ex-intro/ex-doc.tex} 227 164f7a7 start new repo miekg authored May 9, 2010 228 \cleardoublepage 229 \section{Answers} 230 \shipoutAnswer