forked from miekg/gobook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-intro.tex
158 lines (133 loc) · 6.13 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
\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 1 is the first stable release of the language Go.
This document and all exercises work with Go 1 -- if not, it's a bug.
The following convention is used throughout this book:
\begin{itemize}
\item Code, keywords and comments are displayed in \prog{Source Code Pro};
\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 (if needed) 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{plain bold font};
\item An emphasized paragraph is indented and has a vertical bar on the
left.
\end{itemize}
\section{Official documentation}
There is already 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
\prog{go doc}.}. Reading these documents is
certainly not required, but it is recommended.
Go 1 comes with its own documentation in the form of a program called
\prog{go doc}. If you are interested in the documentation
for the built-ins (see ``\titleref{sec:builtins}'' in the next chapter) you
can fire it up, like so:
\begin{display}
\pr \user{go doc builtin}
\end{display}
How to create your own package documentation is explained in chapter \ref{chap:packages}.
\section{Origins}
Go's syntax was pioneered by Modula-2 family of languages.
Specifically, the language Oberon-2, a successor of Oberon and Modula-2 (both by
N. Wirth), shows through: The notation for methods is borrowed from there, and
imports and exports work almost exactly the same (but for the notation). Also,
Oberon abandoned the notion of a separate definition ("header") file, and Go
does the same.
The structure and design of interfaces (see chapter \ref{chap:interfaces}) was
inspired by how object-orientation works in Smalltalk and similar languages,
and personal language experiments done by the Go Authors. It was also based
on the growing consensus in the OO world that inheritance (as it was commonly
implemented) was to be considered harmful.
The whole of idea of using channels (see chapter \ref{chap: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}.
If we dig deeper into the history of Go we also find references
to ``Newsqueak'' \cite{newsqueak}, which pioneered the use of
channel communication in a C--like language. Channels
aren't unique to these languages; a big non--C--like
language which uses them is Erlang \cite{erlang}.
\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{Getting Go}
In this section we explain how to install Go locally on your machine, but you can
also compile Go code online at \url{http://play.golang.org/}. To quickly
play with code this is by far the easiest route.
You can also get pre-compiled binaries from \cite{go_install}.
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 have to 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 latest release (= Go 1) 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 at the end:
\begin{display}
--- cd ../test
0 known bugs; 0 unexpected bugs
ALL TESTS PASSED
---
Installed Go for linux/amd64 in /home/go
Installed commands in /home/go/bin
\end{display}
\section{Getting Go for Windows}
The best way is to follow the instructions from \cite{go_install}, which are repeated
here for your convience.
\begin{itemize}
\item Download Go 1 from:
\url{http://code.google.com/p/go/downloads/list?q=OpSys-Windows+Type%3DArchive};
\item Unpack it to your \verb|C:\| drive;
\item Make sure that the contents are \verb|C:\Go|. Note: this directory should be
created when you unpacked the zip;
\item Add \verb|C:\Go\bin| to your \$PATH:
\begin{display}
set PATH=\verb|%PATH%;C:\Go\bin|
\end{display}
\end{itemize}
\section{Exercises}
\input{ex-intro/ex-doc.tex}
\cleardoublepage
\section{Answers}
\shipoutAnswer