diff --git a/go-beyond.tex b/go-beyond.tex index 07a4ec0..d2bfd80 100644 --- a/go-beyond.tex +++ b/go-beyond.tex @@ -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 @@ -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) @@ -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}