Skip to content

Commit

Permalink
More tweaks and some deletions
Browse files Browse the repository at this point in the history
  • Loading branch information
miekg committed May 17, 2014
1 parent db5033e commit 0fb57c3
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions go-beyond.tex
Original file line number Diff line number Diff line change
Expand Up @@ -307,22 +307,21 @@ \subsection{Methods}
declarations. Also see \cite[section~``Type Declarations'']{go_spec}.
Suppose we have:
\begin{lstlisting}
// A Mutex is a data type with two methods,
// Lock and Unlock.
// A Mutex is a data type with two methods, Lock and Unlock.
type Mutex struct { /* Mutex fields */ }
func (m *Mutex) Lock() { /* Lock impl. */ }
func (m *Mutex) Unlock() { /* Unlock impl. */ }
\end{lstlisting}
We now create two types in two different manners:
\begin{itemize}
\item \lstinline|type NewMutex Mutex|;
\item \lstinline|type PrintableMutex struct { Mutex }|.
\item{\lstinline|type NewMutex Mutex|};
\item{\lstinline|type PrintableMutex struct{Mutex}|}.
\end{itemize}
Now \var{NewMutex} is equal to \var{Mutex}, but
\var{NewMutex} is equal to \var{Mutex}, but
it \emph{does not} have \emph{any} of the methods of \var{Mutex}. In other words
its method set is empty.
But \var{PrintableMutex} \emph{has} \first{\emph{inherited}}{methods!inherited} the
method set from \var{Mutex}.
method set from \var{Mutex}. The Go term for this is \first{\emph{embedding}}{structures!embed}.
In the words of \cite{go_spec}:
\begin{quote}
The method set of \var{*PrintableMutex} contains the methods
Expand Down Expand Up @@ -369,8 +368,7 @@ \section{Conversions}
\item{
From a slice of bytes or runes to a \lstinline{string}.
\begin{lstlisting}
b := []byte{'h','e','l','l','o'} // Composite
// literal
b := []byte{'h','e','l','l','o'} |\coderemark{Composite literal}|
s := string(b)
i := []rune{257,1024,65}
r := string(i)
Expand Down Expand Up @@ -415,12 +413,6 @@ \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

0 comments on commit 0fb57c3

Please sign in to comment.