Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 238 lines (214 sloc) 9.054 kb
7811b5c @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
1 \epi{``I am interested in this and hope to do something.''}
1f704ac @miekg sorta fix small caps
authored
2 {\textit{On adding complex numbers to Go}\\ \textsc{KEN THOMPSON}}
30324e7 @miekg added google logo
authored
3
f4a1ee3 @miekg add a small preface
authored
4 \noindent{}What is Go? From the website \cite{go_web}:
7ae1471 @miekg final phase for 0.2
authored
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 @miekg Spell check
authored
9 that get the most out of multi core and networked machines, while its
7ae1471 @miekg final phase for 0.2
authored
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 @miekg change the introduction a bit (order)
authored
16
c41e6c1 @miekg Tell about Go1
authored
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 @miekg final phase for 0.2
authored
20
21 The following convention is used throughout this book:
164f7a7 @miekg start new repo
authored
22 \begin{itemize}
c5c6e34 @miekg Correct font in graphics (dejavu)
authored
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 @miekg add more stuff about defer
authored
26 \item Extra remarks in the code \coderemark{Are displayed like this};
24a8471 @miekg small style changes
authored
27 \item Longer remarks get a number -- \gocircle{1} -- with the explanation following;
57068d9 @miekg style fixes
authored
28 \item Line numbers are printed on the right side;
c80be2c @miekg Use letters for footnotes
authored
29 \item Shell examples use a \pr{} as prompt;
bc1bf00 @miekg some minor changes
authored
30 \item User entered text in shell examples \texttt{\user{is in bold}}, system responses
31 are in a \texttt{typewriter font};
278d4d4 @miekg spell check
authored
32 \item An emphasized paragraph is indented and has a vertical bar on the
c80be2c @miekg Use letters for footnotes
authored
33 left.
164f7a7 @miekg start new repo
authored
34 \end{itemize}
35
36 \section{Official documentation}
fb87c1c @miekg JCs comments for chapter 1
authored
37 There already is a substantial amount of documentation written about Go.
7811b5c @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
38 \gomarginpar{When searching on the internet use the term ``golang'' instead of plain ``go''.}
fb87c1c @miekg JCs comments for chapter 1
authored
39 The Go Tutorial \cite{go_tutorial}, and the Effective Go
164f7a7 @miekg start new repo
authored
40 document \cite{effective_go}. The
fb87c1c @miekg JCs comments for chapter 1
authored
41 website \url{http://golang.org/doc/} is a very good starting point
8966fda @miekg more proof reading fixes
authored
42 for reading up on Go\footnote{\url{http://golang.org/doc/} itself is served by
7811b5c @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
43 a Go program called \prog{go\ doc}.}. Reading these documents is
353124c @miekg extend the todo
authored
44 certainly not required, but is recommended.
164f7a7 @miekg start new repo
authored
45
7811b5c @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
46 Go comes with its own documentation in the form of a program called
f01b58c @miekg Use go doc instead of godoc
authored
47 \prog{go doc}.
7ae1471 @miekg final phase for 0.2
authored
48 You can use it yourself to look
5c25b47 @miekg Spell check
authored
49 in the on-line documentation. For
7ae1471 @miekg final phase for 0.2
authored
50 instance, suppose we want to know more about the package \package{hash}.
f01b58c @miekg Use go doc instead of godoc
authored
51 We would then give the command \prog{go doc hash}.
eb27712 @miekg remove another bunch of todos
authored
52 How to create your own package documentation
7ae1471 @miekg final phase for 0.2
authored
53 is explained in chapter \ref{chap:packages}.
54
3ab1e13 @miekg Update the intro chapter
authored
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 @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
78 to ``Newsqueak'' \cite{newsqueak}, which pioneered the use of
3ab1e13 @miekg Update the intro chapter
authored
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 @miekg First set of improvements
authored
102 \section{Getting Go}
3ab1e13 @miekg Update the intro chapter
authored
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 @miekg add a note about packages that exist
authored
107 Ubuntu and Debian both have a Go package in their repositories, look for
7811b5c @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
108 the package ``golang''. But there are still some minor issues being worked
cb09cad @miekg add a note about packages that exist
authored
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 @miekg First set of improvements
authored
112 Go yourself. For other Unix like systems the procedure is the same.
164f7a7 @miekg start new repo
authored
113 \begin{itemize}
fb87c1c @miekg JCs comments for chapter 1
authored
114 \item First install Mercurial (to get the \prog{hg} command). In
164f7a7 @miekg start new repo
authored
115 Ubuntu/Debian/Fedora you must install the \prog{mercurial} package;
116
1e848fe @miekg more tweaks to the first 2 chapters
authored
117 \item For building Go you need the packages: \prog{bison},
164f7a7 @miekg start new repo
authored
118 \prog{gcc}, \prog{libc6-dev}, \prog{ed}, \prog{gawk} and \prog{make};
119
9d694e4 @miekg more fixes for the intro chapter
authored
120 \item Set the environment variable \prog{GOROOT} to the root of your
121 Go install:
122 \begin{display}
961db26 @miekg Allow for ~
authored
123 \pr \user{export GOROOT=/go}
9d694e4 @miekg more fixes for the intro chapter
authored
124 \end{display}
125
164f7a7 @miekg start new repo
authored
126 \item Then retrieve the Go source code:
e454eba @miekg better spacing
authored
127 \begin{display}
091ca7a @miekg Some shell examples updates
authored
128 \pr \user{hg clone -r release https://go.googlecode.com/hg/ $GOROOT}
e454eba @miekg better spacing
authored
129 \end{display}
b26ce31 @miekg clear up the first chapter
authored
130
9d694e4 @miekg more fixes for the intro chapter
authored
131 \item Set your PATH to so that the Shell can find the Go binaries:
b26ce31 @miekg clear up the first chapter
authored
132 \begin{display}
091ca7a @miekg Some shell examples updates
authored
133 \pr \user{export PATH=$GOROOT/bin:$PATH}
b26ce31 @miekg clear up the first chapter
authored
134 \end{display}
164f7a7 @miekg start new repo
authored
135 \item Compile Go
e454eba @miekg better spacing
authored
136 \begin{display}
091ca7a @miekg Some shell examples updates
authored
137 \pr \user{cd $GOROOT/src}
138 \pr \user{./all.bash}
e454eba @miekg better spacing
authored
139 \end{display}
164f7a7 @miekg start new repo
authored
140 \end{itemize}
b4d7f06 @miekg Fix use of \user in display's
authored
141 If all goes well, you should see the following at the end:
9d694e4 @miekg more fixes for the intro chapter
authored
142 \begin{display}
b4d7f06 @miekg Fix use of \user in display's
authored
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 @miekg more fixes for the intro chapter
authored
151 \end{display}
152 You now have Go installed on your system and you can start playing.
6390061 @miekg More talk about the versions
authored
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 @mikespook quoting stuff
mikespook authored
155 upgrade to the latest weekly, see the section ``\titleref{sec:weekly}''.
9d694e4 @miekg more fixes for the intro chapter
authored
156
c8f8b29 @miekg add the Windows text from Paulo Pinto
authored
157 \section{Getting Go for Windows}
0637de0 @miekg Some more updates from Paulo
authored
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 @miekg add the Windows text from Paulo Pinto
authored
161 \begin{itemize}
162 \item Download the latest version from \cite{go_windows};
163 \item Unpack it to your \verb|C:\| drive;
0637de0 @miekg Some more updates from Paulo
authored
164 \item Make sure that the contents are \verb|C:\MinGW|. Note: this directory should be
165 created when you unpacked MinGW;
c8f8b29 @miekg add the Windows text from Paulo Pinto
authored
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 @miekg Small tweaks
authored
186 the GOBIN and PATH variables. Note that the GOROOT variable is also included here.
c8f8b29 @miekg add the Windows text from Paulo Pinto
authored
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 @miekg Small tweaks
authored
192 export GOROOT=/c/go
c8f8b29 @miekg add the Windows text from Paulo Pinto
authored
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 @miekg More talk about the versions
authored
203 \label{sec:weekly}
9d694e4 @miekg more fixes for the intro chapter
authored
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 @miekg Some shell examples updates
authored
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 @miekg more fixes for the intro chapter
authored
212 \end{display}
d05e417 @miekg remove stuff
authored
213 \noindent{}To see what you are running right now:
0f213da @miekg Fixes from the code checker
authored
214 \begin{display}
091ca7a @miekg Some shell examples updates
authored
215 \pr \user{cd $GOROOT}
216 \pr \user{hg identify}
0f213da @miekg Fixes from the code checker
authored
217 79997f0e5823 release/release.2010-10-20
218 \end{display}
bc1bf00 @miekg some minor changes
authored
219 \noindent{}That would be release \gorelease{2010-10-20}. The release as
7811b5c @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
220 describe is a ``stable'' releases,
221 as opposed to the ``weekly'' releases that are more volatile.
bc1bf00 @miekg some minor changes
authored
222 If you want to track the weekly releases
2102ba7 @miekg more typos
authored
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 @miekg start section on gofix
authored
231
164f7a7 @miekg start new repo
authored
232 \section{Exercises}
16a6b17 @miekg add another question
authored
233 \input{ex-intro/ex-doc.tex}
234
164f7a7 @miekg start new repo
authored
235 \cleardoublepage
236 \section{Answers}
237 \shipoutAnswer
Something went wrong with that request. Please try again.