forked from miekg/gobook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction.tex
35 lines (34 loc) · 1.6 KB
/
function.tex
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
\begin{lstlisting}[caption=A function declaration,label=src:function definition]
|\begin{tikzpicture}[overlay]
\ubrace{0.6,-1.5}{0.0,-1.5}{The keyword \key{func} is used to declare a function;}
%
\ubrace{2.2,-1.5}{0.8,-1.5}{A function can be defined to work on a specific type, a %
more common name for such a function is \index{method}{method}. This part is %
called a \first{\emph{receiver}}{receiver} and it is optional. This %
will be used in chapter \ref{chap:interfaces};}
%
\ubrace{3.4,-1.5}{2.4,-1.5}{\emph{funcname} is the name of your function;}
%
\ubrace{4.5,-1.5}{3.6,-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{6.0,-1.5}{4.9,-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 %
"\titleref{sec:multiple return}" on page \pageref{sec:multiple return}. %
If you want the return %
parameters not to be named you only give the types: %
\lstinline{(int,int)}. If you have only one value to return you may omit %
the parentheses. If your function is a subroutine and does not have %
anything to return you may omit this entirely;}
%
\ubrace{8.2,-1.5}{6.3,-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}|
type mytype int |\coderemark{New type, see chapter \ref{chap:beyond}}|
func (p mytype) funcname(q int) (r,s int) { return 0,0 }
||
\end{lstlisting}