-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.html
351 lines (333 loc) · 17.4 KB
/
index.html
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
---
layout: page
---
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 1</div>
<div class="title">Syntax Basics</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch01-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch01.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>Are you ready to turbo-charge your software?</b> Do you want a simple programming language that <em>compiles fast</em>? That <em>runs fast</em>? That makes it <em>easy</em> to distribute your work to users? Then you're ready for Go!
</p>
<p>
Go is a programming language that focuses on <em>simplicity</em> and <em>speed</em>. It's <em>simpler</em> than other languages, so it's <em>quicker</em> to learn. And it lets you harness the power of today's multi-core computer processors, so your programs run faster. This chapter will show you all the Go features that will make your life as a developer <em>easier</em>, and make your users <em>happier</em>.
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 2</div>
<div class="title">Conditionals and Loops</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch02-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch02.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>Every program has parts that only apply in certain situations.</b> "This code should run <em>if</em> there's an error. <em>Otherwise</em>, that code should run." Almost every program contains code that should be run only when a certain <em>condition</em> is true. So almost every programming language provides <code>if</code> statements that let you determine whether to run segments of code. Go is no exception.
</p>
<p>
You may also need some parts of your code to run <em>repeatedly</em>. Like most languages, Go provides <code>for</code> loops that run sections of code more than once. We'll learn to use both <code>if</code> and <code>for</code> in this chapter!
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 3</div>
<div class="title">Functions</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch03-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch03.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>It's your turn.</b> You've been calling functions like a pro. But the only functions you could call were the ones Go defined for you. In this chapter, we're going to show you how to <em>create your own functions</em>. We'll learn how to declare functions with and without parameters. We'll declare functions that return a single value, and we'll learn how to <em>return multiple values</em> so that we can indicate when there's been an error. And we'll learn about <em>pointers</em>, which allow us to make more memory-efficient function calls.
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 4</div>
<div class="title">Packages</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch04-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch04.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>It's time to get organized.</b> So far, we've been throwing all our code together in a single file. As our programs grow bigger and more complex, that's going to quickly become a mess.
</p>
<p>
In this chapter, we'll show you how to <em>create your own packages</em> to help keep related code <em>together in one place</em>. But packages are good for more than just organization. Packages are an easy way to <em>share code between your programs</em>. And they're an easy way to <em>share code with other developers</em>.
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 5</div>
<div class="title">Arrays</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch05-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch05.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>A whole lot of programs deal with lists of things.</b> Lists of addresses. Lists of phone numbers. Lists of products. Go has two ways of storing lists built-in. This chapter will introduce the first: arrays. You'll learn about how to <em>create arrays</em>, how to <em>fill them with data</em>, and how to <em>get that data back</em> out again. Then you'll learn about processing all the elements in array, first the hard way with <code>for</code> loops, and then the <em>easy</em> way with <code>for ... range</code> loops.
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 6</div>
<div class="title">Slices</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch06-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch06.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>We've learned we can't add more elements to an array.</b> That's a real problem for our program, because we don't know in advance how many pieces of data our file contains. But that's where Go <em>slices</em> come in. Slices are a collection type that can <em>grow to hold additional items</em> — just the thing to fix our program! We'll also see how slices can provide an easier way for <em>users to provide data</em> to all your programs, and how they can help you write functions that are more <em>convenient to call</em>.
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 7</div>
<div class="title">Maps</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch07-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch07.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>Throwing things in piles is fine, until you need to find something again.</b> You've already seen how to create lists of values using arrays and slices. You've seen how to apply the same operation to every value in an array or slice. But what if you need to <em>work with a particular item</em>? To find it, you'll have to start at the beginning of the array or slice, and look through Every. Single. Value.
</p>
<p>
What if there were a kind of collection where <em>every value had a label</em> on it? You could quickly find <em>just the value you needed</em>! In this chapter, we'll look at <em>maps</em>, which do just that.
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 8</div>
<div class="title">Structs</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch08-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch08.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>Sometimes you need to store more than one type of data.</b> We learned about slices, which store a list of values. Then we learned about maps, which map a list of keys to a list of values. But both of these data structures can only hold values of <em>one type</em>. Sometimes, you need to group together values of <em>several</em> types. Think of mailing addresses, where you have to mix street names (strings) with postal codes (integers). Or student records, where you have to mix student names (strings) with grade point averages (floating-point numbers). You can't mix value types in slices or maps. But you can if you use another type called a <em>struct</em>. We'll learn all about structs in this chapter!
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 9</div>
<div class="title">Defined Types</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch09-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch09.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>There's more to learn about defined types.</b> In the previous chapter, we showed you how to define a type with a struct underlying type. What we <em>didn't</em> show you was that you can use <em>any type</em> as an underlying type.
</p>
<p>
And do you remember methods — the special kind of function that's associated with values of a particular type? We've been calling methods on various values throughout the book, but we haven't shown you how to define your <em>own</em> methods. In this chapter, <em>we're going to fix all of that</em>. Let's get started!
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 10</div>
<div class="title">Encapsulation and Embedding</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch10-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch10.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>Mistakes happen.</b> Sometimes, your program will receive invalid data, from user input, a file you're reading in, or elsewhere. In this chapter, you'll learn about encapsulation: a way to <em>protect your struct type's fields</em> from that invalid data. That way you'll know your field data is <em>safe</em> to work with!
</p>
<p>
We'll also show you how to <em>embed</em> other types within your struct type. If your struct type needs methods that already exist on another type, you <em>don't</em> have to copy and paste the method code. You can embed the other type within your struct type, and then use the embedded type's methods <em>just as if they were defined on your own type</em>!
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 11</div>
<div class="title">Interfaces</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch11-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch11.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>Sometimes you don't care about the particular type of a value.</b> You don't care about <em>what it is</em>. You just need to know that it will be able to <em>do certain things</em>. That you'll be able to call certain methods on it. You don't care whether you have a <code>Pen</code> or a <code>Pencil</code>, you just need something with a <code>Draw</code> method. You don't care whether you have a <code>Car</code> or a <code>Boat</code>, you just need something with a <code>Steer</code> method.
</p>
<p>
That's what Go interfaces accomplish. They let you define variables and function parameters that will hold <em>any type</em>, as long as that type defines <em>certain methods</em>.
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 12</div>
<div class="title">Recovering from Failure</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch12-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch12.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>Every program encounters errors. You should plan for them.</b> Sometimes handling an error can be as simple as reporting it and exiting the program. But other errors may require <em>additional action</em>. You may need to close opened files or network connections, or otherwise clean up, so your program doesn't leave a mess behind. In this chapter, we'll show you how to <em>defer</em> cleanup actions so they happen <em>even when there's an error</em>. We'll also show you how to make your program <em>panic</em> in those (rare) situations where it's appropriate, and how to <em>recover</em> afterwards.
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 13</div>
<div class="title">Goroutines and Channels</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch13-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch13.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>Working on one thing at a time isn't always the fastest way to finish a task.</b> Some big problems can be broken into smaller tasks. <em>Goroutines</em> let your program <em>work on several different tasks at once</em>. Your goroutines can <em>coordinate their work</em> using <em>channels</em>, which let them <em>send data to each other</em> and <em>synchronize</em> so that one goroutine doesn't get ahead of another. Goroutines let you take full advantage of computers with multiple processors, so that your programs run as <em>fast as possible</em>!
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 14</div>
<div class="title">Automated Testing</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch14-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch14.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>Are you sure your software is working right now? Really sure?</b> Before you sent that new version to your users, you presumably tried out the new features to ensure they all worked. But did you try the old features to ensure you didn’t break any of them? <em>All</em> the old features? If that question makes you worry, your program needs <em>automated testing</em>. Automated tests ensure your program’s components <em>work correctly</em>, even after you change your code. Go's <code>testing</code> package and <code>go test</code> tool make it easy to write automated tests, using only the skills that you've already learned!
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 15</div>
<div class="title">Web Apps</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch15-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch15.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>This is the 21st century. Users want web apps.</b> Go’s got you covered there, too! The Go standard library includes packages to help you <em>host your own web applications</em> and make them <em>accessible from any web browser</em>. So we’re going to spend the final two chapters of the book showing you how to build web apps.
</p>
<p>
The first thing your web app needs is the ability to <em>respond</em> when a browser sends it a request. In this chapter, we'll learn to use the <code>net/http</code> package to do just that.
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Chapter 16</div>
<div class="title">HTML Templates</div>
</div>
<div class="buttons">
<a href="source/head-first-go-ch16-code.zip" class="button">
<span>Code Samples</span>
</a>
<a href="exercises/ch16.html" class="button">
<span>More Exercises</span>
</a>
</div>
<p>
<b>Your web app needs to respond with HTML, not plain text.</b> Plain text is fine for e-mails and social media posts. But your pages need to be <em>formatted</em>. They need <em>headings</em> and <em>paragraphs</em>. They need <em>forms</em> where your users can submit data to your app. To do any of that, you need <em>HTML code</em>.
</p>
<p>
And eventually, you'll need to <em>insert data</em> into that HTML code. Which is why Go offers the <code>html/template</code> package, a powerful way to include data in your app's HTML responses. Templates are key to building bigger, better web apps, and in this final chapter, we'll show you how to use them!
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Appendix A</div>
<div class="title">Opening Files</div>
</div>
<div class="buttons">
<a href="source/head-first-go-other-code.zip" class="button">
<span>Code Samples</span>
</a>
</div>
<p>
<b>Some programs need to write data to files, not just read data.</b> Throughout the book, when we've wanted to work with files, you had to create them in your text editor for your programs to read. But some programs generate data, and when they do, they need to be able to <em>write data to a file</em>.
</p>
<p>
We used the <code>os.OpenFile</code> function to open a file for writing earlier in the book. But we didn't have space then to fully explore how it worked. In this appendix, we'll show you everything you need to know in order to <em>use</em> <code>os.OpenFile</code> <em>effectively</em>!
</p>
</div>
<div class="chapter">
<div class="heading">
<div class="chapter-number">Appendix B</div>
<div class="title">Six Things We Didn’t Cover</div>
</div>
<div class="buttons">
<a href="source/head-first-go-other-code.zip" class="button">
<span>Code Samples</span>
</a>
</div>
<p>
<b>We've covered a lot of ground!</b> We'll miss you, but before you go, we wouldn't feel right about sending you out into the world without a little more preparation. We've saved <em>six important topics</em> for this appendix.
</p>
</div>