Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 393 lines (351 sloc) 14.58 kb
7811b5c @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
1 \epi{``I'm always delighted by the light touch and stillness of
b6c6468 @miekg use ubercool underbraces
authored
2 early programming languages. Not much text; a lot gets
3 done. Old programs read like quiet conversations
4 between a well-spoken research worker and a well-
5 studied mechanical colleague, not as a debate with a
6 compiler. Who'd have guessed sophistication bought
7811b5c @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
7 such noise?''}{\textsc{RICHARD P. GABRIEL}}
b6c6468 @miekg use ubercool underbraces
authored
8
ba9906a @djotto 'in' -> 'go'
djotto authored
9 \noindent{}Functions are the basic building blocks of Go programs; all interesting
6e05a3d @miekg more stuff for the better
authored
10 stuff happens in them. A function is declared as follows:
80c31b4 @miekg added explanation for functions
authored
11 \input{fig/function.tex}
12 \showremarks
7f05eee @djotto readability
djotto authored
13 Here are two examples. On the left is a function without a return value,
14 while on the right is a simple function that returns its input.
1338834 @miekg put the function next to each other
authored
15
16 \begin{minipage}{.5\textwidth}
b9c8efb @miekg add piece for defer
authored
17 \begin{lstlisting}
18 func subroutine(in int) {
19 return
20 }
21 \end{lstlisting}
1338834 @miekg put the function next to each other
authored
22 \end{minipage}
23 \begin{minipage}{.5\textwidth}
b9c8efb @miekg add piece for defer
authored
24 \begin{lstlisting}
25 func identity(in int) int {
26 return in
27 }
28 \end{lstlisting}
1338834 @miekg put the function next to each other
authored
29 \end{minipage}
440ab24 @miekg typos
authored
30
a2468f8 @djotto readability
djotto authored
31 Functions can be declared in any order you wish. The compiler scans the
32 entire file before execution, so function prototyping is a thing of the
8a15c89 @djotto simplify
djotto authored
33 past in Go. Go disallows nested functions, but you can work around this with
34 anonymous functions. See section ``\titleref{sec:functions as values}'' on page
35 \pageref{sec:functions as values} in this chapter.
17a1170 @miekg add scope figure
authored
36
a5f6b25 @djotto word order
djotto authored
37 Recursive functions work just as in other languages:
f9dde2f @miekg move the average function to the correct place
authored
38 \begin{lstlisting}[caption=Recursive function]
39 func rec(i int) {
40 if i == 10 {
41 return
42 }
43 rec(i+1)
44 fmt.Printf("%d ", i)
45 }
46 \end{lstlisting}
398d508 @miekg tweaks
authored
47 This prints: \texttt{9 8 7 6 5 4 3 2 1 0}.
5400c2f @miekg Fixes in chapter 3
authored
48 %%\newpage %% TODO don't want a newpage here actually
17a1170 @miekg add scope figure
authored
49 \section{Scope}
df075f6 @miekg First 3 chaps done
authored
50 Variables declared outside any functions are \first{global}{scope!local} in Go, those
51 defined in functions are \first{local}{scope!local} to those functions. If names overlap --- a
b9c8efb @miekg add piece for defer
authored
52 local variable is declared with the same name as a global one --- the
02e7dab @miekg Lots of (small) changes by JC
authored
53 local variable hides the global one when the current function is
eb3f994 @miekg need todo scope3
authored
54 executed.
02e7dab @miekg Lots of (small) changes by JC
authored
55
17a1170 @miekg add scope figure
authored
56 \begin{minipage}{.5\textwidth}
57 \input{fig/scope1.tex}
58 \hfill
9670ad2 @miekg more stuff and better
authored
59 \vfill
17a1170 @miekg add scope figure
authored
60 \end{minipage}
609a702 @miekg Make it work with a5paper
authored
61 \hfill
17a1170 @miekg add scope figure
authored
62 \begin{minipage}{.5\textwidth}
63 \input{fig/scope2.tex}
64 \hfill
9670ad2 @miekg more stuff and better
authored
65 \vfill
17a1170 @miekg add scope figure
authored
66 \end{minipage}
67
9670ad2 @miekg more stuff and better
authored
68 In listing \ref{src:scope1} we introduce a local variable \var{a}
02e7dab @miekg Lots of (small) changes by JC
authored
69 in the function \func{q()}.
6c3367f @djotto 'That' -> 'This'
djotto authored
70 The local \var{a} is only visible in \func{q()}. This is
9670ad2 @miekg more stuff and better
authored
71 why the code will print: \texttt{656}.
72 In listing \ref{src:scope2} no new variables are introduced, there
73 is only a global \var{a}.
4273e99 @tonymagro Fixed some small errors.
tonymagro authored
74 Assigning a new value to \var{a} will be globally visible. This code will
9670ad2 @miekg more stuff and better
authored
75 print: \texttt{655}
76
eb3f994 @miekg need todo scope3
authored
77 In the following example we call \func{g()} from \func{f()}:
df075f6 @miekg First 3 chaps done
authored
78
66920e2 @miekg drop the dots at the end of the captions
authored
79 \lstinputlisting[caption=Scope when calling functions from functions]{src/scope3.go}
df075f6 @miekg First 3 chaps done
authored
80
fb7bb8f @djotto 'printout' -> 'output' (printout implies paper)
djotto authored
81 The output will be: \texttt{565}. A \emph{local} variable is \emph{only}
02e7dab @miekg Lots of (small) changes by JC
authored
82 valid when we are executing the function in which it is defined.
df075f6 @miekg First 3 chaps done
authored
83 %%Finally, one can create a \first{"function literal"}{function literal} in which you essentially
84 %%define a function inside another
85 %%function, i.e. a \first{nested function}{nested function}.
86 %%The following figure should clarify why it prints: \texttt{565757}.
87 %%\input{fig/scope3.tex}
eb3f994 @miekg need todo scope3
authored
88
80c31b4 @miekg added explanation for functions
authored
89 \section{Multiple return values}
90 \label{sec:multiple return}
7811b5c @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
91 One of Go's unusual (for compiled languages) features is that functions and methods can return multiple
92 values (Python and Perl can do this too). This can be used to improve on a couple of
02e7dab @miekg Lots of (small) changes by JC
authored
93 clumsy idioms in C programs:
eb3f994 @miekg need todo scope3
authored
94 in-band error returns (such as -1 for \texttt{EOF}) and modifying an argument.
d8e9406 @miekg another review of chapter 3
authored
95 In Go, \lstinline{Write} returns a count and an
7811b5c @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
96 error: ``Yes, you wrote some bytes but not all of them because you filled the
97 device''. The signature of \lstinline{*File.Write} in package
21f65e7 @miekg added stuff
authored
98 \package{os} is:
99 \begin{lstlisting}
d159748 @miekg os.Error is no longer
authored
100 func (file *File) Write(b []byte) (n int, err error)
21f65e7 @miekg added stuff
authored
101 \end{lstlisting}
102 and as the documentation says, it returns the number of bytes written and a
d159748 @miekg os.Error is no longer
authored
103 non-\lstinline{nil} \var{error} when \lstinline{n != len(b)}. This is a common
eb3f994 @miekg need todo scope3
authored
104 style in Go.
21f65e7 @miekg added stuff
authored
105
106 A similar approach obviates the need to pass a pointer to a return value to
107 simulate a reference parameter. Here's a simple-minded function to grab a
108 number from a position in a byte array, returning the number and the next
109 position.
110 \begin{lstlisting}
111 func nextInt(b []byte, i int) (int, int) {
112 x := 0
0cc6318 @miekg add method/function call. Fix references
authored
113 // Naively assume everything is a number
114 for ; i < len(b); i++ {
77e8ddf @djotto gofmt output
djotto authored
115 x = x*10 + int(b[i]) - '0'
21f65e7 @miekg added stuff
authored
116 }
117 return x, i
118 }
119 \end{lstlisting}
0c47c0e @djotto a is a variable
djotto authored
120 You could use it to scan the numbers in an input array \var{a} like this:
21f65e7 @miekg added stuff
authored
121 \begin{lstlisting}
0cc6318 @miekg add method/function call. Fix references
authored
122 a := []byte{'1', '2', '3', '4'}
123 var x int
124 for i := 0; i < len(a); { |\coderemark{No \texttt{i++}}|
125 x, i = nextInt(a, i)
5865984 @djotto println -> fmt.Println
djotto authored
126 fmt.Println(x)
0cc6318 @miekg add method/function call. Fix references
authored
127 }
21f65e7 @miekg added stuff
authored
128 \end{lstlisting}
64c3031 @djotto change wording
djotto authored
129 In the absence of tuples as a native type, multiple return values are the next
357e66e @djotto remove 'to have'
djotto authored
130 best thing. You can return precisely what you want without
eb3f994 @miekg need todo scope3
authored
131 overloading the domain space with special values to signal errors.
b9c8efb @miekg add piece for defer
authored
132
133 \section{Named result parameters}
aa4f15b @miekg add more stuff about defer
authored
134 \label{sec:named result parameters}
df075f6 @miekg First 3 chaps done
authored
135 The return or result parameters of a Go function can be given names and used
21f65e7 @miekg added stuff
authored
136 as regular variables, just like the incoming parameters. When named, they are
56d7fc8 @djotto simplify
djotto authored
137 initialized to the zero values for their types when the function begins. If the
ebf32e8 @miekg multiple tweaks
authored
138 function executes a \key{return} statement with no arguments, the current values of
56d7fc8 @djotto simplify
djotto authored
139 the result parameters are returned. Using these
c80be2c @miekg Use letters for footnotes
authored
140 features enables you (again) to do more with less code \footnote{This is
7811b5c @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
141 a motto of Go; ``Do \emph{more} with \emph{less} code''}.
21f65e7 @miekg added stuff
authored
142
ebf32e8 @miekg multiple tweaks
authored
143 The names are not mandatory but they can make code shorter and clearer:
144 \emph{they are documentation}.
145 If we name the results of \lstinline{nextInt} it becomes obvious which
146 returned \type{int} is which.
21f65e7 @miekg added stuff
authored
147
148 \begin{lstlisting}
f08ef0b @miekg more automatic code checking
authored
149 func nextInt(b []byte, pos int) (value, nextPos int) { /* ... */ }
21f65e7 @miekg added stuff
authored
150 \end{lstlisting}
151 Because named results are initialized and tied to an unadorned
152 \key{return},
153 they can simplify as well as clarify. Here's a version of
d159748 @miekg os.Error is no longer
authored
154 \lstinline{io.ReadFull} that uses them well:
21f65e7 @miekg added stuff
authored
155
156 \begin{lstlisting}
d159748 @miekg os.Error is no longer
authored
157 func ReadFull(r Reader, buf []byte) (n int, err error) {
21f65e7 @miekg added stuff
authored
158 for len(buf) > 0 && err == nil {
159 var nr int
160 nr, err = r.Read(buf)
161 n += nr
162 buf = buf[nr:len(buf)]
163 }
164 return
165 }
166 \end{lstlisting}
164f7a7 @miekg start new repo
authored
167
b9c8efb @miekg add piece for defer
authored
168 \section{Deferred code}
2674427 @miekg add another function
authored
169 \label{sec:deferred code}
b9c8efb @miekg add piece for defer
authored
170 Suppose you have a function in which you open a file and perform various
171 writes and reads on it. In such a function there are often spots where
eb3f994 @miekg need todo scope3
authored
172 you want to return early. If you do that, you will need to close the file
b9c8efb @miekg add piece for defer
authored
173 descriptor you are working on. This often leads to the following code:
5a25657 @miekg Now style stuff in captions
authored
174 \begin{lstlisting}[caption=Without defer]
f08ef0b @miekg more automatic code checking
authored
175 func ReadWrite() bool {
eb3f994 @miekg need todo scope3
authored
176 file.Open("file")
f6c393d @miekg Another bunch of fixes from: Filip Zaludek
authored
177 // Do your thing
b9c8efb @miekg add piece for defer
authored
178 if failureX {
5400c2f @miekg Fixes in chapter 3
authored
179 file.Close() |\coderemark{}|
b9c8efb @miekg add piece for defer
authored
180 return false
181 }
182
183 if failureY {
5400c2f @miekg Fixes in chapter 3
authored
184 file.Close() |\coderemark{}|
b9c8efb @miekg add piece for defer
authored
185 return false
186 }
5400c2f @miekg Fixes in chapter 3
authored
187 file.Close() |\coderemark{}|
b9c8efb @miekg add piece for defer
authored
188 return true
189 }
190 \end{lstlisting}
6d42d8e @djotto reorder words
djotto authored
191 A lot of code is repeated here. To overcome this Go has the
df075f6 @miekg First 3 chaps done
authored
192 \first{\key{defer}}{keyword!defer} statement. After
0b4fcec @djotto reword
djotto authored
193 \key{defer} you specify a function which is called just \emph{before}
194 the current function exits.
b9c8efb @miekg add piece for defer
authored
195
02e7dab @miekg Lots of (small) changes by JC
authored
196 The code above could be rewritten as follows. This makes the
aa4f15b @miekg add more stuff about defer
authored
197 function more readable, shorter and puts the \func{Close} right next
198 to the \func{Open}.
5a25657 @miekg Now style stuff in captions
authored
199 \begin{lstlisting}[caption=With defer]
f08ef0b @miekg more automatic code checking
authored
200 func ReadWrite() bool {
eb3f994 @miekg need todo scope3
authored
201 file.Open("file")
5400c2f @miekg Fixes in chapter 3
authored
202 defer file.Close() |\coderemark{\func{file.Close()} is added to the defer list}|
f6c393d @miekg Another bunch of fixes from: Filip Zaludek
authored
203 // Do your thing
b9c8efb @miekg add piece for defer
authored
204 if failureX {
f08ef0b @miekg more automatic code checking
authored
205 return false |\coderemark{\func{Close()} is now done automatically}|
b9c8efb @miekg add piece for defer
authored
206 }
207 if failureY {
f08ef0b @miekg more automatic code checking
authored
208 return false |\coderemark{And here too}|
b9c8efb @miekg add piece for defer
authored
209 }
210 return true
211 }
212 \end{lstlisting}
7811b5c @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
213 You can put multiple functions on the ``deferred list''\index{deferred list}, like this
eb3f994 @miekg need todo scope3
authored
214 example from \cite{effective_go}:
18eade9 @miekg more
authored
215 \begin{lstlisting}
216 for i := 0; i < 5; i++ {
217 defer fmt.Printf("%d ", i)
218 }
219 \end{lstlisting}
220 Deferred functions are executed in LIFO order, so the above code
aa4f15b @miekg add more stuff about defer
authored
221 prints: \lstinline{4 3 2 1 0}.
222
223 With \func{defer} you can even change return values, provided that
df075f6 @miekg First 3 chaps done
authored
224 you are using named result parameters and a function
eb27712 @miekg remove another bunch of todos
authored
225 literal\index{function!literal}\footnote{A function literal
226 is sometimes called a \index{closure} closure.}, i.e:
66920e2 @miekg drop the dots at the end of the captions
authored
227 \begin{lstlisting}[caption=Function literal]
aa4f15b @miekg add more stuff about defer
authored
228 defer func() {
f08ef0b @miekg more automatic code checking
authored
229 /* ... */
d44ae4d @miekg more changes
authored
230 }() |\coderemark{() is needed here}|
aa4f15b @miekg add more stuff about defer
authored
231 \end{lstlisting}
bcdbb27 @miekg Add parameters to the function.
authored
232 Or this example which makes it easier to understand why and where
233 you need the braces:
234 \begin{lstlisting}[caption=Function literal with parameters]
235 defer func(x int) {
236 /* ... */
237 }(5) |\coderemark{Give the input variable \var{x} the value 5}|
238 \end{lstlisting}
acf0672 @djotto that -> this
djotto authored
239 In this (unnamed) function you can access any named return
02e7dab @miekg Lots of (small) changes by JC
authored
240 parameter:
5a25657 @miekg Now style stuff in captions
authored
241 \begin{lstlisting}[caption=Access return values within defer]
f08ef0b @miekg more automatic code checking
authored
242 func f() (ret int) { |\coderemark{\var{ret} is initialized with zero}|
aa4f15b @miekg add more stuff about defer
authored
243 defer func() {
a537d58 @miekg small fixes
authored
244 ret++ |\coderemark{Increment \var{ret} with 1}|
aa4f15b @miekg add more stuff about defer
authored
245 }()
df075f6 @miekg First 3 chaps done
authored
246 return 0 |\coderemark{1 \emph{not} 0 will be returned!}|
aa4f15b @miekg add more stuff about defer
authored
247 }
248 \end{lstlisting}
249
b9c8efb @miekg add piece for defer
authored
250 \section{Variadic parameters}
7c8a0ec @djotto simplify
djotto authored
251 Functions that take a variable number of parameters are known as variadic functions.
252 To declare a function as variadic:
d05e417 @miekg remove stuff
authored
253 \begin{lstlisting}
96ac511 @miekg passing variadic args to other functions
authored
254 func myfunc(arg ...int) {}
f800954 @miekg more on variadac args
authored
255 \end{lstlisting}
7f5ac4d @djotto gofmt-style
djotto authored
256 The \lstinline{arg ...int} instructs Go to see this as a function that
02e7dab @miekg Lots of (small) changes by JC
authored
257 takes a variable number of arguments. Note that these arguments all
f800954 @miekg more on variadac args
authored
258 have the type \type{int}. Inside your function's body the variable
d02c2fa @miekg fix prural of types (ints, strings) dont make the first part bold
authored
259 \var{arg} is a slice of ints:
f800954 @miekg more on variadac args
authored
260 \begin{lstlisting}
261 for _, n := range arg {
262 fmt.Printf("And the number is: %d\n", n)
263 }
264 \end{lstlisting}
c29f528 @miekg Spell check
authored
265 If you don't specify the type of the variadic argument it defaults to the
7add098 @miekg fix references n stuff
authored
266 empty interface \var{interface\{\}} (see chapter
fd23f89 @dgryski frm: remove extra double-quote
dgryski authored
267 \ref{chap:interfaces}).
96ac511 @miekg passing variadic args to other functions
authored
268 Suppose we have another variadic function called \func{myfunc2}, the
6616d50 @djotto remove the
djotto authored
269 following example shows how to pass variadic arguments to it:
96ac511 @miekg passing variadic args to other functions
authored
270 \begin{lstlisting}
271 func myfunc(arg ...int) {
272 myfunc2(arg...) |\coderemark{Pass it as-is}|
273 myfunc2(arg[:2]...) |\coderemark{Slice it}|
274 }
275 \end{lstlisting}
21f65e7 @miekg added stuff
authored
276
cd19815 @miekg add anonymous functions
authored
277 \section{Functions as values}
278 \label{sec:functions as values}
df075f6 @miekg First 3 chaps done
authored
279 \index{function!as values}
280 \index{function!literals}
02e7dab @miekg Lots of (small) changes by JC
authored
281 As with almost everything in Go, functions are also \emph{just} values.
282 They can be assigned to variables as follows:
d8e9406 @miekg another review of chapter 3
authored
283 \lstinputlisting[label=src:anonfunc,caption=Anonymous function,linerange={3,}]{src/anon-func.go}
02e7dab @miekg Lots of (small) changes by JC
authored
284 If we use \lstinline{fmt.Printf("%T\n", a)} to print the type of
285 \var{a}, it prints \func{func()}.
cd19815 @miekg add anonymous functions
authored
286
1f8dae0 @djotto simplify
djotto authored
287 Functions--as--values may be used in other places, for example maps.
df075f6 @miekg First 3 chaps done
authored
288 Here we convert from integers to functions:
17f2abb @miekg lots of updates
authored
289 \begin{lstlisting}[caption=Functions as values in maps]
02e7dab @miekg Lots of (small) changes by JC
authored
290 var xs = map[int]func() int{
291 1: func() int { return 10 },
292 2: func() int { return 20 },
f08ef0b @miekg more automatic code checking
authored
293 3: func() int { return 30 }, |\coderemark{Mandatory ,}|
294 /* ... */
295 }
fc00628 @miekg functions as values expanded
authored
296 \end{lstlisting}
a84fec3 @miekg add higher order Map function
authored
297 Or you can write a function that takes a function as its parameter, for
8966fda @miekg more proof reading fixes
authored
298 example a \func{Map} function that works on \type{int} slices. This is
5710de8 @djotto comma -> brackets
djotto authored
299 left as an exercise for the reader (see exercise Q\ref{ex:map function}
300 on page \pageref{ex:map function}).
fc00628 @miekg functions as values expanded
authored
301
b39f672 @miekg kill that section
authored
302 \section{Callbacks}
16121c9 @miekg Add more
authored
303 \label{sec:callbacks}
1f8dae0 @djotto simplify
djotto authored
304 Because functions are values they are easy to pass to functions, from where
4301f50 @miekg more on callbacks
authored
305 they can be used as callbacks. First define a function that
7811b5c @miekg Fix all the quotes and more [from: Stefan Schroeder]
authored
306 does ``something'' with an integer value:
4301f50 @miekg more on callbacks
authored
307 \begin{lstlisting}
066f416 @miekg small fixes
authored
308 func printit(x int) { |\coderemark{Function returns nothing}|
f60072a @djotto Printf() not Print()
djotto authored
309 fmt.Printf("%v\n", x) |\coderemark{Just print it}|
4301f50 @miekg more on callbacks
authored
310 }
311 \end{lstlisting}
312 The signature of this function is: \lstinline{func printit(int)}, or
066f416 @miekg small fixes
authored
313 without the function name: \mbox{\lstinline{func(int)}}. To create a new function
4301f50 @miekg more on callbacks
authored
314 that uses this one as a callback we need to use this signature:
315 \begin{lstlisting}
066f416 @miekg small fixes
authored
316 func callback(y int, f func(int)) { |\coderemark{\func{f} will hold the function}|
4301f50 @miekg more on callbacks
authored
317 f(y) |\coderemark{Call the callback \func{f} with \var{y}}|
318 }
319 \end{lstlisting}
16121c9 @miekg Add more
authored
320
74f9941 @miekg start work on panic/recover
authored
321 \section{Panic and recovering}
ccd7f2b @miekg more typos fixed
authored
322 \label{sec:panic}
1f8dae0 @djotto simplify
djotto authored
323 Go does not have an exception mechanism, like that in Java for instance: you cannot throw exceptions.
4273e99 @tonymagro Fixed some small errors.
tonymagro authored
324 Instead it uses a panic-and-recover mechanism. It is worth remembering that you should use this as
832d3d4 @miekg more on panic and recover
authored
325 a last resort, your code will not look, or be, better if it is littered with panics. It's a powerful tool:
440ab24 @miekg typos
authored
326 use it wisely. So, how do you use it?
832d3d4 @miekg more on panic and recover
authored
327
328 The following description was taken from \cite{go_blog_panic}:
329 \begin{description}
330 \item[Panic]{is a built-in function that stops the ordinary flow of control and begins panicking. When the function
331 \func{F} calls \key{panic},
332 execution of \func{F} stops, any deferred functions in \func{F} are executed normally, and
333 then \func{F} returns to its caller. To the caller, \func{F} then
334 behaves like a call to \key{panic}. The process continues up the stack until all functions in the current
335 goroutine have returned, at which point the program crashes.
336
337 Panics can be initiated by invoking \func{panic} directly. They can also be caused by \emph{runtime errors}, such
338 as out-of-bounds array accesses.}
339
340 \item[Recover]{is a built-in function that regains control of a panicking goroutine. Recover is \emph{only} useful inside
40195c3 @miekg emph the right word
authored
341 \emph{deferred} functions.
832d3d4 @miekg more on panic and recover
authored
342
343 During normal execution, a call to \func{recover} will return \type{nil} and have no other effect.
344 If the current goroutine is panicking, a call
345 to \func{recover} will capture the value given to \func{panic} and resume normal execution.}
346 \end{description}
74f9941 @miekg start work on panic/recover
authored
347
52b759e @miekg typos
authored
348 This function checks if the function it gets as argument will panic when it is
349 executed\footnote{Copied from a presentation of Eleanor McHugh.}:
350 \begin{lstlisting}
351 func throwsPanic(f func()) (b bool) { |\longremark{We define a new function \func{throwsPanic} that takes %
e3636cd @djotto clarity
djotto authored
352 a function as an argument (see ``\titleref{sec:functions as values}''). It returns true if \func{f} panics %
353 when run, else false;}|
aa36d10 @djotto Sentence break
djotto authored
354 defer func() { |\longremark{We define a \func{defer} function that utilizes \func{recover}. If the %
31b826c @miekg typos
authored
355 current goroutine panics, this defer function will notice that. If \func{recover()} returns non-\var{nil} we set \var{b} %
52b759e @miekg typos
authored
356 to true;}|
357 if x := recover(); x != nil {
358 b = true
359 }
360 }()
5400c2f @miekg Fixes in chapter 3
authored
361 f() |\longremark{Execute the function we received as the argument;}|
52b759e @miekg typos
authored
362 return |\longremark{Return the value of \var{b}. Because \var{b} is a named return parameter (page %
31b826c @miekg typos
authored
363 \pageref{sec:named result parameters}), we don't specify \var{b}.}|
52b759e @miekg typos
authored
364 }
365 \end{lstlisting}
366 \showremarks
367
368
1480620 @miekg add more stuff
authored
369 \section{Exercises}
8144196 @miekg more stuff moved around
authored
370 \input{ex-functions/ex-average.tex}
371
02e7dab @miekg Lots of (small) changes by JC
authored
372 \input{ex-functions/ex-order.tex}
373
3134bde @miekg More comments from JC. This time for chapter 04
authored
374 \input{ex-functions/ex-scope.tex}
375
ab3a85f @miekg added figure and exercises
authored
376 \input{ex-functions/ex-stack.tex}
377
81e1b36 @miekg tweaks and making it better
authored
378 \input{ex-functions/ex-vararg.tex}
379
7fcac65 @miekg Move around the fibonaci exercise and add one in channels chapter
authored
380 \input{ex-functions/ex-fib.tex}
381
691d301 @miekg put map exercise in the function chapter
authored
382 \input{ex-functions/ex-map.tex}
383
8144196 @miekg more stuff moved around
authored
384 \input{ex-functions/ex-minmax.tex}
385
1730bc1 @miekg move bubblesort to functions
authored
386 \input{ex-functions/ex-bubblesort.tex}
387
2674427 @miekg add another function
authored
388 \input{ex-functions/ex-funcfunc.tex}
389
ab3a85f @miekg added figure and exercises
authored
390 \cleardoublepage
391 \section{Answers}
392 \shipoutAnswer
Something went wrong with that request. Please try again.