Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 231 lines (209 sloc) 8.58 kB
7225623 @miekg added quotes in the epis
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
c80be2c @miekg Use letters for footnotes
authored
17 Go is a young language, where
18 features are still being added or even \emph{removed}. It
3dd4e3f @miekg change the introduction a bit (order)
authored
19 may be possible that some text is outdated when you
c80be2c @miekg Use letters for footnotes
authored
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 @miekg change the introduction a bit (order)
authored
24 date with respect to the latest Go release.
b26ce31 @miekg clear up the first chapter
authored
25 An effort has been made to create "future proof" code examples.
7ae1471 @miekg final phase for 0.2
authored
26
27 The following convention is used throughout this book:
164f7a7 @miekg start new repo
authored
28 \begin{itemize}
c5c6e34 @miekg Correct font in graphics (dejavu)
authored
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 @miekg add more stuff about defer
authored
32 \item Extra remarks in the code \coderemark{Are displayed like this};
24a8471 @miekg small style changes
authored
33 \item Longer remarks get a number -- \gocircle{1} -- with the explanation following;
57068d9 @miekg style fixes
authored
34 \item Line numbers are printed on the right side;
c80be2c @miekg Use letters for footnotes
authored
35 \item Shell examples use a \pr{} as prompt;
bc1bf00 @miekg some minor changes
authored
36 \item User entered text in shell examples \texttt{\user{is in bold}}, system responses
37 are in a \texttt{typewriter font};
278d4d4 @miekg spell check
authored
38 \item An emphasized paragraph is indented and has a vertical bar on the
c80be2c @miekg Use letters for footnotes
authored
39 left.
164f7a7 @miekg start new repo
authored
40 \end{itemize}
41
42 \section{Official documentation}
fb87c1c @miekg JCs comments for chapter 1
authored
43 There already is a substantial amount of documentation written about Go.
c729bfa @miekg indentation tweaks
authored
44 \gomarginpar{When searching on the internet use the term "golang" instead of plain "go".}
fb87c1c @miekg JCs comments for chapter 1
authored
45 The Go Tutorial \cite{go_tutorial}, and the Effective Go
164f7a7 @miekg start new repo
authored
46 document \cite{effective_go}. The
fb87c1c @miekg JCs comments for chapter 1
authored
47 website \url{http://golang.org/doc/} is a very good starting point
8966fda @miekg more proof reading fixes
authored
48 for reading up on Go\footnote{\url{http://golang.org/doc/} itself is served by
353124c @miekg extend the todo
authored
49 a Go program called \prog{godoc}.}. Reading these documents is
50 certainly not required, but is recommended.
164f7a7 @miekg start new repo
authored
51
7ae1471 @miekg final phase for 0.2
authored
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 @miekg Spell check
authored
55 in the on-line documentation. For
7ae1471 @miekg final phase for 0.2
authored
56 instance, suppose we want to know more about the package \package{hash}.
57 We would then give the command \prog{godoc hash}.
eb27712 @miekg remove another bunch of todos
authored
58 How to create your own package documentation
7ae1471 @miekg final phase for 0.2
authored
59 is explained in chapter \ref{chap:packages}.
60
0f58f2a @miekg First set of improvements
authored
61 \section{Getting Go}
cb09cad @miekg add a note about packages that exist
authored
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 @miekg First set of improvements
authored
67 Go yourself. For other Unix like systems the procedure is the same.
164f7a7 @miekg start new repo
authored
68 \begin{itemize}
fb87c1c @miekg JCs comments for chapter 1
authored
69 \item First install Mercurial (to get the \prog{hg} command). In
164f7a7 @miekg start new repo
authored
70 Ubuntu/Debian/Fedora you must install the \prog{mercurial} package;
71
1e848fe @miekg more tweaks to the first 2 chapters
authored
72 \item For building Go you need the packages: \prog{bison},
164f7a7 @miekg start new repo
authored
73 \prog{gcc}, \prog{libc6-dev}, \prog{ed}, \prog{gawk} and \prog{make};
74
9d694e4 @miekg more fixes for the intro chapter
authored
75 \item Set the environment variable \prog{GOROOT} to the root of your
76 Go install:
77 \begin{display}
091ca7a @miekg Some shell examples updates
authored
78 \pr \user{export GOROOT=~/go}
9d694e4 @miekg more fixes for the intro chapter
authored
79 \end{display}
80
164f7a7 @miekg start new repo
authored
81 \item Then retrieve the Go source code:
e454eba @miekg better spacing
authored
82 \begin{display}
091ca7a @miekg Some shell examples updates
authored
83 \pr \user{hg clone -r release https://go.googlecode.com/hg/ $GOROOT}
e454eba @miekg better spacing
authored
84 \end{display}
b26ce31 @miekg clear up the first chapter
authored
85
9d694e4 @miekg more fixes for the intro chapter
authored
86 \item Set your PATH to so that the Shell can find the Go binaries:
b26ce31 @miekg clear up the first chapter
authored
87 \begin{display}
091ca7a @miekg Some shell examples updates
authored
88 \pr \user{export PATH=$GOROOT/bin:$PATH}
b26ce31 @miekg clear up the first chapter
authored
89 \end{display}
164f7a7 @miekg start new repo
authored
90
91 \item Compile Go
e454eba @miekg better spacing
authored
92 \begin{display}
091ca7a @miekg Some shell examples updates
authored
93 \pr \user{cd $GOROOT/src}
94 \pr \user{./all.bash}
e454eba @miekg better spacing
authored
95 \end{display}
164f7a7 @miekg start new repo
authored
96 \end{itemize}
9d694e4 @miekg more fixes for the intro chapter
authored
97 If all goes well, you should see the following:
98 \begin{display}
091ca7a @miekg Some shell examples updates
authored
99 Installed Go for linux/amd64 in /home/go.
100 Installed commands in /home/go/bin.
9d694e4 @miekg more fixes for the intro chapter
authored
101 The compiler is 6g.
102 \end{display}
103 You now have Go installed on your system and you can start playing.
104
c8f8b29 @miekg add the Windows text from Paulo Pinto
authored
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 @miekg more fixes for the intro chapter
authored
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 @miekg Some shell examples updates
authored
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 @miekg more fixes for the intro chapter
authored
156 \end{display}
d05e417 @miekg remove stuff
authored
157 \noindent{}To see what you are running right now:
0f213da @miekg Fixes from the code checker
authored
158 \begin{display}
091ca7a @miekg Some shell examples updates
authored
159 \pr \user{cd $GOROOT}
160 \pr \user{hg identify}
0f213da @miekg Fixes from the code checker
authored
161 79997f0e5823 release/release.2010-10-20
162 \end{display}
bc1bf00 @miekg some minor changes
authored
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 @miekg more typos
authored
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 @miekg start section on gofix
authored
175
26ca6e0 @miekg move origin section
authored
176 \section{Origins}
245d0e5 @miekg I stand corrected
authored
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 @miekg clear up the first chapter
authored
179 \cite{limbo}. Quoting from the Limbo paper:
a4c7402 @miekg check it
authored
180 \begin{quote}
181 Limbo is a programming language intended for applications running
182 distributed systems on small computers. It supports modular programming,
691d301 @miekg put map exercise in the function chapter
authored
183 strong type checking at compile- and run-time, \emph{inter process
3a340cb @miekg 98% finished for the first 2 chaps
authored
184 communication over typed channels}, automatic \emph{garbage collection}, and
a4c7402 @miekg check it
authored
185 simple abstract data types. It is designed for safe execution even on
186 small machines without hardware memory protection.
187 \end{quote}
1e848fe @miekg more tweaks to the first 2 chapters
authored
188 One feature of Limbo that is
189 included in Go is the support for cross compiling.
b26ce31 @miekg clear up the first chapter
authored
190 Another feature Go inherited from Limbo is channels (see chapter
9d694e4 @miekg more fixes for the intro chapter
authored
191 \ref{chap:channels}). Again from the Limbo documentation.
2eccb75 @miekg add stuff about gos history
authored
192 \begin{quote}
66c2efe @miekg two typos
authored
193 [A channel] is a communication mechanism capable of sending and receiving objects of
2eccb75 @miekg add stuff about gos history
authored
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 @miekg clear up the first chapter
authored
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 @miekg add chrono fig for Go
authored
206 \begin{figure}[H]
66920e2 @miekg drop the dots at the end of the captions
authored
207 \caption{Chronology of Go}
95b6c9c @miekg add chrono fig for Go
authored
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 @miekg clear up the first chapter
authored
214 The whole of idea of using channels to communicate with other processes
215 is called Communicating Sequential Processes (CSP) and was conceived
fb87c1c @miekg JCs comments for chapter 1
authored
216 by C. A. R. Hoare \cite{hoare}, who incidentally is the same man that
5400c2f @miekg Fixes in chapter 3
authored
217 invented QuickSort \cite{quicksort}.
a4c7402 @miekg check it
authored
218
a6f96cd @miekg spacing stuff
authored
219 \begin{lbar}[]
f800954 @miekg more on variadac args
authored
220 Go is the first C--like language that is widely available,
3a340cb @miekg 98% finished for the first 2 chaps
authored
221 runs on many
b26ce31 @miekg clear up the first chapter
authored
222 different platforms and makes concurrency easy (or easier).
223 \end{lbar}
7bb76f6 @miekg Give credit to the people how are helping
authored
224
164f7a7 @miekg start new repo
authored
225 \section{Exercises}
16a6b17 @miekg add another question
authored
226 \input{ex-intro/ex-doc.tex}
227
164f7a7 @miekg start new repo
authored
228 \cleardoublepage
229 \section{Answers}
230 \shipoutAnswer
Something went wrong with that request. Please try again.