Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
213 lines (190 sloc) 8.59 KB
\epi{"I am interested in this and hope to do something."}
{\textit{On adding complex numbers to Go}\\ \textsc{KEN THOMPSON}}
\noindent{}This is an introduction to the Go language from Google. Its aim
is to provide a guide to this new and innovative language. What is
Go? From the website \cite{go_web}:
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 multicore 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.
The intended audience of this book is people who are familiar with programming
and know multiple programming languages, be it C\cite{c}, C++\cite{c++},
Perl \cite{perl}, Java \cite{java}, Erlang\cite{erlang}, Scala\cite{scala} or
Haskell\cite{haskell}. This is \emph{not} a book which teaches you how to
program, this is a book that just teaches you how to use Go.
As with
learning new things, probably the best way to do this is to discover it for
yourself by creating your own programs.
Therefor includes each chapter a number of exercises (and answers)
to acquaint you with the language.
An exercise
is numbered as \textbf{Q$n$}, where $n$ is a number. After the
exercise number another number in parentheses displays the difficulty
of this particular assignment. This difficulty ranges from 0 to
9, where 0 is easy and 9 is difficult.
Then a short name is given, for easier reference.
For example
\textbf{Q1}. (1) A map function \ldots
introduces a question numbered \textbf{Q1} of a level 1 difficulty, concerning a
\func{map()}-function. The answers are included after the exercises on a
new page, except for those pesky unresolved research problems.
The numbering and setup of the answers is identical to the
exercises, except that an answer starts with \textbf{A$n$}, where the
number $n$ corresponds with the number of the exercise.
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:
\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 An emphasized paragraph is indented and has a vertical bar on the
\section{Official documentation}
\gomarginpar{When searching on the internet use the term "golang".}
There already is a substantial amount of documentation written about Go.
The Go Tutorial \cite{go_tutorial}, and the Effective Go
document \cite{effective_go}. The
website \url{} is a very good starting point
for reading up on Go\footnote{\url{} 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
You can use it yourself to look
in the online 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}
There are currently (2011) no (official) packages for Go in any Linux
distribution. The route to install Go is thus slightly longer than
it could be. When Go stabilizes this situation will change. For
now, you need to retrieve the code from the mercurial archive and compile
Go yourself. For other Unix like systems the procedure is the same.
\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:
\pr \user{export GOROOT=~/go}
\item Then retrieve the Go source code:
\pr \user{hg clone -r release $GOROOT}
\item Set your PATH to so that the Shell can find the Go binaries:
\pr \user{export PATH=$GOROOT/bin:$PATH}
\item Compile Go
\pr \user{cd $GOROOT/src}
\pr \user{./all.bash}
If all goes well, you should see the following:
Installed Go for linux/amd64 in /home/go.
Installed commands in /home/go/bin.
The compiler is 6g.
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:
\pr \user{cd $GOROOT}
\pr \user{hg pull}
\pr \user{hg update release}
\pr \user{cd src}
\pr \user{./all.bash}
\noindent{}To see what you are running right now:
\pr \user{cd $GOROOT}
\pr \user{hg identify}
79997f0e5823 release/release.2010-10-20
\noindent{}That would be \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 \texttt{\pr hg update weekly} instead of \texttt{\pr hg update release}.
If there a changes that require changes to old(er) programs you can use
the included program \prog{gofix} to automatically update the syntax. See
section "\titleref{sec:gofix}" in chapter \ref{chap:misc}.
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:
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.
One feature of Limbo that is
included in Go is the support for cross compiling.
\todo{Alef language on Plan 9. Also presented in 1995.}
Another feature Go inherited from Limbo is channels (see chapter
\ref{chap:channels}). Again from the Limbo documentation.
[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.
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}.
\caption{Chronology of Go}
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}.
Go is the first C--like language that is widely available,
runs on many
different platforms and makes concurrency easy (or easier).