# public miekg /gobook

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Fetching contributors…

Cannot retrieve contributors at this time

file 177 lines (152 sloc) 6.885 kb
 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 \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 makeprogrammers more productive. Go is expressive, concise, clean, andefficient. Its concurrency mechanisms make it easy to write programsthat get the most out of multi core and networked machines, while itsnovel type system enables flexible and modular program construction. Gocompiles quickly to machine code yet has the convenience of garbagecollection and the power of run-time reflection. It's a fast, staticallytyped, 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 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 responsesare in a \texttt{typewriter font};\item An emphasized paragraph is indented and has a vertical bar on theleft.\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 Godocument \cite{effective_go}. Thewebsite \url{http://golang.org/doc/} is a very good starting pointfor reading up on Go\footnote{\url{http://golang.org/doc/} itself is served by \prog{go doc}.}. Reading these documents iscertainly 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 documentationfor the built-ins (see \titleref{sec:builtins}'' in the next chapter) youcan 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 has it origins in Inferno \cite{inferno} (which in turn was basedupon 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 runningdistributed systems on small computers. It supports modular programming,strong type checking at compile- and run-time, \emph{inter processcommunication over typed channels}, automatic \emph{garbage collection}, andsimple abstract data types. It is designed for safe execution even onsmall machines without hardware memory protection.\end{quote}A 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 ofthe specified type to another agent in the system. Channels may be usedto communicate between local processes; using library procedures, theymay be connected to named destinations. In either case send and receiveoperations may be directed to them.\end{quote}The channels in Go are easier to use than those in Limbo.If we dig deeper into the history of Go we also find referencesto Newsqueak'' \cite{newsqueak}, which pioneered the use of channel communication in a C--like language. Channelsaren't unique to these languages; a big non--C--likelanguage which 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 processesis called Communicating Sequential Processes (CSP) and was conceivedby C. A. R. Hoare \cite{hoare}, who incidentally is the same man thatinvented QuickSort \cite{quicksort}.\begin{lbar}[]Go is the first C--like language that is widely available,runs on manydifferent 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 canalso compile Go code online at \url{http://play.golang.org/}. To quicklyplay 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 forthe package golang''), but there are still some minor issues being workedout. For now we will stick to the installation from source.So we will have to retrieve the code from the mercurial archive and compileGo yourself. For other Unix-like systems the procedure is the same.\begin{itemize}\item First install Mercurial (to get the \prog{hg} command). InUbuntu/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 yourGo 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 ../test0 known bugs; 0 unexpected bugsALL TESTS PASSED---Installed Go for linux/amd64 in /home/goInstalled 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 repeatedhere 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 becreated 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
Something went wrong with that request. Please try again.