Skip to content

Commit

Permalink
Merge branch 'master' of github.com:miekg/gobook
Browse files Browse the repository at this point in the history
  • Loading branch information
miekg committed Nov 14, 2013
2 parents 61f73c5 + 6fc5c82 commit 5712539
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 107 deletions.
29 changes: 0 additions & 29 deletions Changelog

This file was deleted.

3 changes: 2 additions & 1 deletion coderemarks.sty
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
%% define 2 commands
%% \longremark[1] where you can say something about the code
%% \showremarks - displays all remarks in a list after the code

\newcounter{coderemarks}
\setcounter{coderemarks}{0}
\newcounter{codevar}
\setcounter{codevar}{0}
%
\newcommand{\gocircle}[1]{%
\tikz\node[text=white,font=\sffamily\bfseries,inner sep=0.2mm,draw,circle,fill=black]{#1};}
\tikz\node[text=white,font=\sffamily\bfseries,minimum size=3mm,inner sep=0mm,draw,circle,fill=black]{\small #1};}
%
\newcommand{\longremark}[1]{%
\gocircle{\arabic{coderemarks}}%
Expand Down
6 changes: 3 additions & 3 deletions ex-basics/ex-for.tex
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
\begin{lstlisting}
func main() {
i := 0 |\coderemark{Define our loop variable}|
I: |\coderemark{Define a label}|
Loop: |\coderemark{Define a label}|
fmt.Printf("%d\n", i)
i++
if i < 10 {
goto I |\coderemark{Jump to the label}|
i++
goto Loop |\coderemark{Jump to the label}|
}
}
\end{lstlisting}
Expand Down
8 changes: 5 additions & 3 deletions ex-basics/ex-strings.tex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
In addition, make it output the number of bytes in that string.
\emph{Hint}: Check out the \package{utf8} package.

\Question \label{ex:string q3} Extend the program from
\Question \label{ex:string q3} Extend/change the program from
the previous question to replace the three runes at
position 4 with 'abc'.

Expand Down Expand Up @@ -57,8 +57,10 @@
\lstinputlisting[label=src:string2,caption=Runes in strings]{ex-basics/src/string2.go}
\end{minipage}

\Question Left as an exercise for the reader. And if you do it, please let me know! I will then
put your answer here.
\Question Something along the lines of:
\begin{minipage}{\textwidth}
\lstinputlisting[label=src:string3]{ex-basics/src/string3.go}
\end{minipage}

\Question Reversing a string can be done as follows. We start from the left (\var{i}) and
the right (\var{j}) and swap the characters as we see them:
Expand Down
13 changes: 13 additions & 0 deletions ex-basics/src/string3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"fmt"
)

func main() {
s := "Шла Саша по шоссе"
r := []rune(s)
copy(r[4:4+3], []rune("abc"))
fmt.Printf("Before: %s\n", s);
fmt.Printf("After : %s\n", string(r))
}
8 changes: 4 additions & 4 deletions fig/function.tex
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
This is called the \first{\emph{receiver}}{receiver}. A function with a receiver is %
a \index{method}{method}. This will be explored in chapter \ref{chap:interfaces};}
%
\ubrace{4.7,-1.5}{3.1,-1.5}{\emph{funcname} is the name of your function;}
\ubrace{4.6,-1.5}{3.1,-1.5}{\emph{funcname} is the name of your function;}
%
\ubrace{6.1,-1.5}{4.9,-1.5}{The variable \var{q} of type \type{int} is %
\ubrace{5.9,-1.5}{4.7,-1.5}{The variable \var{q} of type \type{int} is %
the input parameter. The parameters are passed %
\first{\emph{pass-by-value}}{pass-by-value} meaning they are copied;}
%
\ubrace{8.1,-1.5}{6.4,-1.5}{%
\ubrace{7.7,-1.5}{6.1,-1.5}{%
The variables \var{r} and \var{s} are the %
\index{named return parameters}{named return parameters} for this function. %
Functions in Go can have multiple return values, see section %
Expand All @@ -23,7 +23,7 @@
the parentheses. If your function is a subroutine and does not have %
anything to return you may omit this entirely;}
%
\ubrace{11.1,-1.5}{8.4,-1.5}{This is the function's body. Note that %
\ubrace{10.7,-1.5}{8.0,-1.5}{This is the function's body. Note that %
\func{return} is a statement so the braces around the parameter(s) are %
optional.}
\end{tikzpicture}|
Expand Down
11 changes: 4 additions & 7 deletions fig/reflection.tex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\begin{lstlisting}[caption=Introspection using reflection,label=src:introspection]
|\begin{tikzpicture}[overlay]
\ubrace{3.2,-5.2}{2.2,-5.2}{%
\ubrace{3.2,-4.2}{2.2,-4.2}{%
We are dealing with a \type{Type} and according %
to the documentation\footnote{\texttt{go doc reflect}}:%
\begin{quote} %
Expand All @@ -10,11 +10,11 @@
\end{quote} %
So on \var{t} we use \func{Elem()} to get the value the pointer %
points to;}%
\ubrace{4.9,-5.2}{3.8,-5.2}{We have now dereferenced the pointer %
\ubrace{4.9,-4.2}{3.8,-4.2}{We have now dereferenced the pointer %
and are "inside" our structure. %
We now use \func{Field(0)} to access the zeroth field;}%
%
\ubrace{6.4,-5.2}{5.8,-5.2}{%
\ubrace{6.0,-4.2}{5.1,-4.2}{%
The struct \type{StructField} has a \var{Tag} member which %
returns the tag-name as a string. So on the $0^{th}$ field we can %
unleash \func{.Tag} to access this name: \texttt{Field(0).Tag}. This %
Expand All @@ -26,10 +26,7 @@
age int
}

p1 := new(Person)|\coderemark{\func{new} returns a pointer to Person}|
ShowTag(p1)|\coderemark{\func{ShowTag()} is called with this pointer}|

func ShowTag(i interface{}) {
func ShowTag(i interface{}) { |\coderemark{Called with *Person}|
switch t := reflect.TypeOf(i); t.Kind() {
case reflect.Ptr: |\coderemark{A pointer, thus \var{reflect.Ptr}}|
tag := t.Elem().Field(0).Tag
Expand Down
2 changes: 1 addition & 1 deletion fig/scope1.tex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}

func q() {
a := 5|\coderemark{Definition}|
a := 5 |\coderemark{Definition}|
println(a)
}
\end{lstlisting}
39 changes: 6 additions & 33 deletions go-basics.tex
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,6 @@ \section{Official documentation}

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}

There are a few things that make Go different from other languages.
\begin{description}
\item[Clean and Simple]
Expand Down Expand Up @@ -127,7 +96,6 @@ \section{Origins}

\item[Fun]
Programming with Go should be fun!

\end{description}
Erlang \cite{erlang} also shares some
of the features of Go. Notable differences between Erlang
Expand All @@ -136,6 +104,11 @@ \section{Origins}
machine, while Go is compiled. Go also has a much more Unix-like
feel to it.

\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{Hello World}
\label{sec:hello world}
In the Go tutorial, Go is presented to the world in the typical
Expand All @@ -144,7 +117,7 @@ \section{Hello World}
the 1970s). We don't think we can do better, so
here it is, ``Hello World'' in Go.

\lstinputlisting[numbers=right,label=src:hello,caption=Hello world]{src/helloworld.go}
\lstinputlisting[label=src:hello,caption=Hello world]{src/helloworld.go}
Lets look at the program line by line.
\showremarks

Expand Down
9 changes: 7 additions & 2 deletions go-beyond.tex
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ \subsection{Allocation with make}
These examples illustrate the difference between \key{new} and
\key{make}.
\begin{lstlisting}
var p *[]int = new([]int) |\coderemark{Allocates slice structure; \var{*p == nil}}|
|\coderemark{Rarely useful}|
var p *[]int = new([]int) |\coderemark{Allocates slice structure;rarely useful}|
var v []int = make([]int, 100) |\coderemark{\var{v} refers to a new array of 100 ints}|

var p *[]int = new([]int) |\coderemark{Unnecessarily complex}|
Expand Down Expand Up @@ -416,6 +415,12 @@ \subsection{User defined types and conversions}
\type{int} also fails; an integer is not the same as a structure containing
an integer.

\section{Composition}
TODO(miek):work in progress
Go isn't an object oriented language and as such does not have inheritance, but sometimes you
do want to "inherit" some methods that are implemented for a type and change a few. In Go is
this possible by embedding a type.

\section{Exercises}
\input{ex-beyond/ex-pointer-arith.tex}

Expand Down
1 change: 1 addition & 0 deletions go-contributors.tex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
\emph{Adam J. Gray},
\emph{Alex Sychev},
\emph{Alexey Chernenkov},
\emph{Andrea Spadaccini},
\emph{Andrey Mirtchovski},
\emph{Anthony Magro},
Expand Down
2 changes: 1 addition & 1 deletion go-packages.tex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
outside your package (more on that later).
Now we just need to build the package. We create a directory under \var{\$GOPATH},
and copy \file{even.go} there (see ``\titleref{sec:building a program}'' in chapter \ref{chap:basics}).
and copy \file{even.go} there (see ``\titleref{sec:building a program}'' in chapter \ref{chap:intro}).
\begin{display}
\pr \user{mkdir $GOPATH/src/even}
Expand Down
20 changes: 3 additions & 17 deletions go-setup.tex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
\setmonofont[Path=fonts/,Scale=0.95,BoldFont={SourceCodePro-Bold}]{SourceCodePro-Regular}
\setmonofont[Path=fonts/,Scale=0.90,BoldFont={SourceCodePro-Bold}]{SourceCodePro-Regular}
%% Fontfeatures after setting the mono font to keep
%% lstlisting fonts happy
%%\defaultfontfeatures{Scale=MatchLowercase,Mapping=tex-text}
Expand All @@ -12,19 +12,10 @@
\setlength{\parindent}{0pt}
\setlength{\parskip}{0.7ex plus 0.5ex minus 0.2ex}

%%\setlength{\parskip}{0.3\baselineskip}
%%\setlength{\parindent}{0pt}

%% list of answers
\newlistof{listofex}{ex}{List of Exercises}
\newlistentry{exercise}{ex}{0}

\newlistof{listofcode}{code}{List of Code Examples}
\newlistentry{code}{code}{0}

%% need to do this for code examples too
%%\renewcommand{\lstlistlistingname}{List of Code Examples}
%% toc
\renewcommand{\tocheadstart}{}
\renewcommand{\aftertoctitle}{\pagestyle{blocks}}
\renewcommand{\aftertoctitle}{\thispagestyle{empty}\afterchaptertitle\pagestyle{blocks}}
Expand All @@ -51,11 +42,6 @@
\renewcommand{\afterextitle}{\thispagestyle{blocks}}
\renewcommand{\printextitle}[1]{\section*{#1}}
\renewcommand{\exmark}{\markboth{\myfamily \typename: \contentsname}{\myfamily List of Exercises}}
%% code
\renewcommand{\codeheadstart}{}
\renewcommand{\aftercodetitle}{\thispagestyle{blocks}}
\renewcommand{\printcodetitle}[1]{\section*{#1}}
\renewcommand{\codemark}{\markboth{\myfamily \typename: \contentsname}{\myfamily List of Code Examples}}

\nobibintoc
\renewcommand*{\indexmark}{%
Expand All @@ -71,7 +57,7 @@
\expandafter\def\expandafter\quote\expandafter{\quote\em}
%% Listings
\newfontfamily\listingsfont[Path=fonts/,Scale=0.95,BoldFont={SourceCodePro-Bold}]{SourceCodePro-Regular}
\newfontfamily\listingsfont[Path=fonts/,Scale=0.90,BoldFont={SourceCodePro-Bold}]{SourceCodePro-Regular}
\lstdefinelanguage{Go}
{morekeywords={append,break,cap,case,chan,const,continue,copy,default,defer,else,fallthrough,%
Expand All @@ -94,7 +80,7 @@
\lstset{language=Go,inputencoding=utf8,extendedchars=false,texcl,escapechar=\|,basicstyle=\ttfamily,keywordstyle=\normalfont,numbers=none,numberblanklines=false,showstringspaces=false,breaklines=true,numberstyle=\small\ttfamily,xleftmargin=\parindent,xrightmargin=1em,linewidth=0.98\linewidth}
%,literate={"}{\textasciiquote}{1}}
\newcommand{\coderemark}[1]{\sffamily\qquad$\leftarrow \textit{\small #1}$}
\newcommand{\coderemark}[1]{\sffamily\qquad$\leftarrow \textit{\footnotesize #1}$}
%% Cite style
%%\bibpunct{[}{]}{;}{s}{,}{,}
Expand Down
4 changes: 1 addition & 3 deletions go_a4.tex
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,14 @@

\pagenumbering{roman}
\tableofcontents*
\listoffigures*
%%\listoftables* %% there are so few
\listofcode*
\listofex*
\clearpage

\chapter*{Preface}
\label{chap:preface}
\input{go-preface.tex}

\pagenumbering{arabic}
%% renamed from Basics
\chapter{Introduction}
\label{chap:intro}
Expand Down
5 changes: 2 additions & 3 deletions src/helloworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main |\longremark{This first line is just required. All Go files start w
\mbox{\lstinline{package <something>}}, \lstinline{package main} is required for a %
standalone executable;}|

import "fmt" // Implements formatted I/O. \longremark{This says we need \package{fmt} in %
import "fmt" |\longremark{This says we need \package{fmt} in %
addition to \package{main}. A package other than \package{main} is commonly called a %
library, a familiar concept in many programming languages (see chapter \ref{chap:packages}). %
The line ends with a comment which is started with \lstinline|//|;}
The line ends with a comment which is started with \lstinline|//|;}|// Implements formatted I/O.

/* Print something */ |\longremark{This is also a comment, but this one is enclosed in %
\lstinline|/*| and \lstinline|*/|;}|
Expand All @@ -22,4 +22,3 @@ string to the screen. The string is enclosed with \lstinline{"} and may %
contain non-ASCII characters. Here we use Greek and Japanese.}|
fmt.Printf("Hello, world; or |$\kappa\alpha\lambda\eta\mu\acute{\epsilon}\rho\alpha\hspace{1em}\kappa$\'o$ \sigma\mu\epsilon$|; or |こんにちは 世界|")
}

0 comments on commit 5712539

Please sign in to comment.