forked from miekg/gobook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-intro.tex
188 lines (169 loc) · 7.08 KB
/
go-intro.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
\epi{"I am interested in this and hope to do something."}
{\textit{On adding complex numbers to Go}\\ \textsc{KEN THOMPSON}}
\noindent{}What is Go? From the website \cite{go_web}:
\begin{quote}
The Go programming language is an open source project to make
programmers more productive. Go is expressive, concise, clean, and
efficient. Its concurrency mechanisms make it easy to write programs
that get the most out of multi core and networked machines, while its
novel type system enables flexible and modular program construction. Go
compiles quickly to machine code yet has the convenience of garbage
collection and the power of run-time reflection. It's a fast, statically
typed, compiled language that feels like a dynamically typed,
interpreted language.
\end{quote}
Go is a young language, where
features are still being added or even \emph{removed}. It
may be possible that some text is outdated when you
read it.
Some exercise answers may become incorrect as Go continues
to evolve.
We will do our best to keep this document up to
date with respect to the latest Go release.
An effort has been made to create "future proof" code examples.
The following convention is used throughout this book:
\begin{itemize}
\item Code is displayed in \prog{DejaVu Mono};
\item Keywords are displayed in \key{DejaVu Mono Bold};
\item Comments are displayed in \rem{DejaVu Mono Italic};
\item Extra remarks in the code \coderemark{Are displayed like this};
\item Longer remarks get a number -- \gocircle{1} -- with the explanation following;
\item Line numbers are printed on the right side;
\item Shell examples use a \pr{} as prompt;
\item User entered text in shell examples \texttt{\user{is in bold}}, system responses
are in a \texttt{typewriter font};
\item An emphasized paragraph is indented and has a vertical bar on the
left.
\end{itemize}
\section{Official documentation}
There already is a substantial amount of documentation written about Go.
\gomarginpar{When searching on the internet use the term "golang" instead of plain "go".}
The Go Tutorial \cite{go_tutorial}, and the Effective Go
document \cite{effective_go}. The
website \url{http://golang.org/doc/} is a very good starting point
for reading up on Go\footnote{\url{http://golang.org/doc/} itself is served by
a Go program called \prog{godoc}.}. Reading these documents is
certainly not required, but is recommended.
Go comes with its own documentation in the form of a Go program called
\prog{godoc}.
You can use it yourself to look
in the on-line documentation. For
instance, suppose we want to know more about the package \package{hash}.
We would then give the command \prog{godoc hash}.
How to create your own package documentation
is explained in chapter \ref{chap:packages}.
\section{Getting Go}
Ubuntu and Debian both have a Go package in their repositories, look for
the package "golang". But there are still some minor issues being worked
out. For now we will stick to the installation from source.
So we will retrieve the code from the mercurial archive and compile
Go yourself. For other Unix like systems the procedure is the same.
\begin{itemize}
\item First install Mercurial (to get the \prog{hg} command). In
Ubuntu/Debian/Fedora you must install the \prog{mercurial} package;
\item For building Go you need the packages: \prog{bison},
\prog{gcc}, \prog{libc6-dev}, \prog{ed}, \prog{gawk} and \prog{make};
\item Set the environment variable \prog{GOROOT} to the root of your
Go install:
\begin{display}
\pr \user{export GOROOT=~/go}
\end{display}
\item Then retrieve the Go source code:
\begin{display}
\pr \user{hg clone -r release https://go.googlecode.com/hg/ $GOROOT}
\end{display}
\item Set your PATH to so that the Shell can find the Go binaries:
\begin{display}
\pr \user{export PATH=$GOROOT/bin:$PATH}
\end{display}
\item Compile Go
\begin{display}
\pr \user{cd $GOROOT/src}
\pr \user{./all.bash}
\end{display}
\end{itemize}
If all goes well, you should see the following:
\begin{display}
Installed Go for linux/amd64 in /home/go.
Installed commands in /home/go/bin.
The compiler is 6g.
\end{display}
You now have Go installed on your system and you can start playing.
\subsection{Keeping up to date}
New releases are announced on the Go Nuts mailing list \cite{go_nuts}. To update an
existing tree to the latest release, you can run:
\begin{display}
\pr \user{cd $GOROOT}
\pr \user{hg pull}
\pr \user{hg update release}
\pr \user{cd src}
\pr \user{./all.bash}
\end{display}
\noindent{}To see what you are running right now:
\begin{display}
\pr \user{cd $GOROOT}
\pr \user{hg identify}
79997f0e5823 release/release.2010-10-20
\end{display}
\noindent{}That would be release \gorelease{2010-10-20}. The release as
describe is a "stable" releases,
as opposed to the "weekly" releases that are more volatile.
If you want to track the weekly releases
instead of the stable ones you can use:
\begin{display}
\pr \user{hg update weekly}
\end{display}
In stead of
\begin{display}
\pr \user{hg update release}
\end{display}
\section{Origins}
Go has it origins in Inferno \cite{inferno} (which in turn was based
upon Plan 9 \cite{plan9}). Inferno included a language called Limbo
\cite{limbo}. Quoting from the Limbo paper:
\begin{quote}
Limbo is a programming language intended for applications running
distributed systems on small computers. It supports modular programming,
strong type checking at compile- and run-time, \emph{inter process
communication over typed channels}, automatic \emph{garbage collection}, and
simple abstract data types. It is designed for safe execution even on
small machines without hardware memory protection.
\end{quote}
One feature of Limbo that is
included in Go is the support for cross compiling.
Another feature Go inherited from Limbo is channels (see chapter
\ref{chap:channels}). Again from the Limbo documentation.
\begin{quote}
[A channel] is a communication mechanism capable of sending and receiving objects of
the specified type to another agent in the system. Channels may be used
to communicate between local processes; using library procedures, they
may be connected to named destinations. In either case send and receive
operations may be directed to them.
\end{quote}
The channels in Go are easier to use than those in Limbo.
If we dig even deeper in the history of Go we also find references
to "Newsqueak" \cite{newsqueak}, which pioneered the use of
channel communication in a C--like language. Channel
communication isn't unique to these languages, a big non--C--like
language which also uses them is Erlang \cite{erlang}.
\begin{figure}[H]
\caption{Chronology of Go}
\label{fig:chrono-of-go}
\begin{center}
\includegraphics[scale=0.65]{fig/go-history.pdf}
\end{center}
\end{figure}
The whole of idea of using channels to communicate with other processes
is called Communicating Sequential Processes (CSP) and was conceived
by C. A. R. Hoare \cite{hoare}, who incidentally is the same man that
invented QuickSort \cite{quicksort}.
\begin{lbar}[]
Go is the first C--like language that is widely available,
runs on many
different platforms and makes concurrency easy (or easier).
\end{lbar}
\section{Exercises}
\input{ex-intro/ex-doc.tex}
\cleardoublepage
\section{Answers}
\shipoutAnswer