blob: f6fe48c8d0465b4892b6c38863825e4976177270 [file] [log] [blame]
Brent Austinba3052e2015-04-21 16:08:23 -07001<!--{
2 "Title": "Effective Go",
3 "Template": true
4}-->
5
6<h2 id="introduction">Introduction</h2>
7
8<p>
9Go is a new language. Although it borrows ideas from
10existing languages,
11it has unusual properties that make effective Go programs
12different in character from programs written in its relatives.
13A straightforward translation of a C++ or Java program into Go
14is unlikely to produce a satisfactory result&mdash;Java programs
15are written in Java, not Go.
16On the other hand, thinking about the problem from a Go
17perspective could produce a successful but quite different
18program.
19In other words,
20to write Go well, it's important to understand its properties
21and idioms.
22It's also important to know the established conventions for
23programming in Go, such as naming, formatting, program
24construction, and so on, so that programs you write
25will be easy for other Go programmers to understand.
26</p>
27
28<p>
29This document gives tips for writing clear, idiomatic Go code.
30It augments the <a href="/ref/spec">language specification</a>,
31the <a href="//tour.golang.org/">Tour of Go</a>,
32and <a href="/doc/code.html">How to Write Go Code</a>,
33all of which you
34should read first.
35</p>
36
37<h3 id="examples">Examples</h3>
38
39<p>
40The <a href="/src/">Go package sources</a>
41are intended to serve not
42only as the core library but also as examples of how to
43use the language.
44Moreover, many of the packages contain working, self-contained
45executable examples you can run directly from the
46<a href="//golang.org">golang.org</a> web site, such as
47<a href="//golang.org/pkg/strings/#example_Map">this one</a> (if
48necessary, click on the word "Example" to open it up).
49If you have a question about how to approach a problem or how something
50might be implemented, the documentation, code and examples in the
51library can provide answers, ideas and
52background.
53</p>
54
55
56<h2 id="formatting">Formatting</h2>
57
58<p>
59Formatting issues are the most contentious
60but the least consequential.
61People can adapt to different formatting styles
62but it's better if they don't have to, and
63less time is devoted to the topic
64if everyone adheres to the same style.
65The problem is how to approach this Utopia without a long
66prescriptive style guide.
67</p>
68
69<p>
70With Go we take an unusual
71approach and let the machine
72take care of most formatting issues.
73The <code>gofmt</code> program
74(also available as <code>go fmt</code>, which
75operates at the package level rather than source file level)
76reads a Go program
77and emits the source in a standard style of indentation
78and vertical alignment, retaining and if necessary
79reformatting comments.
80If you want to know how to handle some new layout
81situation, run <code>gofmt</code>; if the answer doesn't
82seem right, rearrange your program (or file a bug about <code>gofmt</code>),
83don't work around it.
84</p>
85
86<p>
87As an example, there's no need to spend time lining up
88the comments on the fields of a structure.
89<code>Gofmt</code> will do that for you. Given the
90declaration
91</p>
92
93<pre>
94type T struct {
95 name string // name of the object
96 value int // its value
97}
98</pre>
99
100<p>
101<code>gofmt</code> will line up the columns:
102</p>
103
104<pre>
105type T struct {
106 name string // name of the object
107 value int // its value
108}
109</pre>
110
111<p>
112All Go code in the standard packages has been formatted with <code>gofmt</code>.
113</p>
114
115
116<p>
117Some formatting details remain. Very briefly:
118</p>
119
120<dl>
121 <dt>Indentation</dt>
122 <dd>We use tabs for indentation and <code>gofmt</code> emits them by default.
123 Use spaces only if you must.
124 </dd>
125 <dt>Line length</dt>
126 <dd>
127 Go has no line length limit. Don't worry about overflowing a punched card.
128 If a line feels too long, wrap it and indent with an extra tab.
129 </dd>
130 <dt>Parentheses</dt>
131 <dd>
132 Go needs fewer parentheses than C and Java: control structures (<code>if</code>,
133 <code>for</code>, <code>switch</code>) do not have parentheses in
134 their syntax.
135 Also, the operator precedence hierarchy is shorter and clearer, so
136<pre>
137x&lt;&lt;8 + y&lt;&lt;16
138</pre>
139 means what the spacing implies, unlike in the other languages.
140 </dd>
141</dl>
142
143<h2 id="commentary">Commentary</h2>
144
145<p>
146Go provides C-style <code>/* */</code> block comments
147and C++-style <code>//</code> line comments.
148Line comments are the norm;
149block comments appear mostly as package comments, but
150are useful within an expression or to disable large swaths of code.
151</p>
152
153<p>
154The program—and web server—<code>godoc</code> processes
155Go source files to extract documentation about the contents of the
156package.
157Comments that appear before top-level declarations, with no intervening newlines,
158are extracted along with the declaration to serve as explanatory text for the item.
159The nature and style of these comments determines the
160quality of the documentation <code>godoc</code> produces.
161</p>
162
163<p>
164Every package should have a <i>package comment</i>, a block
165comment preceding the package clause.
166For multi-file packages, the package comment only needs to be
167present in one file, and any one will do.
168The package comment should introduce the package and
169provide information relevant to the package as a whole.
170It will appear first on the <code>godoc</code> page and
171should set up the detailed documentation that follows.
172</p>
173
174<pre>
175/*
176Package regexp implements a simple library for regular expressions.
177
178The syntax of the regular expressions accepted is:
179
180 regexp:
181 concatenation { '|' concatenation }
182 concatenation:
183 { closure }
184 closure:
185 term [ '*' | '+' | '?' ]
186 term:
187 '^'
188 '$'
189 '.'
190 character
191 '[' [ '^' ] character-ranges ']'
192 '(' regexp ')'
193*/
194package regexp
195</pre>
196
197<p>
198If the package is simple, the package comment can be brief.
199</p>
200
201<pre>
202// Package path implements utility routines for
203// manipulating slash-separated filename paths.
204</pre>
205
206<p>
207Comments do not need extra formatting such as banners of stars.
208The generated output may not even be presented in a fixed-width font, so don't depend
209on spacing for alignment&mdash;<code>godoc</code>, like <code>gofmt</code>,
210takes care of that.
211The comments are uninterpreted plain text, so HTML and other
212annotations such as <code>_this_</code> will reproduce <i>verbatim</i> and should
213not be used.
214One adjustment <code>godoc</code> does do is to display indented
215text in a fixed-width font, suitable for program snippets.
216The package comment for the
217<a href="/pkg/fmt/"><code>fmt</code> package</a> uses this to good effect.
218</p>
219
220<p>
221Depending on the context, <code>godoc</code> might not even
222reformat comments, so make sure they look good straight up:
223use correct spelling, punctuation, and sentence structure,
224fold long lines, and so on.
225</p>
226
227<p>
228Inside a package, any comment immediately preceding a top-level declaration
229serves as a <i>doc comment</i> for that declaration.
230Every exported (capitalized) name in a program should
231have a doc comment.
232</p>
233
234<p>
235Doc comments work best as complete sentences, which allow
236a wide variety of automated presentations.
237The first sentence should be a one-sentence summary that
238starts with the name being declared.
239</p>
240
241<pre>
Dan Willemsen38f2dba2016-07-08 14:54:35 -0700242// Compile parses a regular expression and returns, if successful,
243// a Regexp that can be used to match against text.
244func Compile(str string) (*Regexp, error) {
Brent Austinba3052e2015-04-21 16:08:23 -0700245</pre>
246
247<p>
248If the name always begins the comment, the output of <code>godoc</code>
249can usefully be run through <code>grep</code>.
250Imagine you couldn't remember the name "Compile" but were looking for
251the parsing function for regular expressions, so you ran
252the command,
253</p>
254
255<pre>
256$ godoc regexp | grep parse
257</pre>
258
259<p>
260If all the doc comments in the package began, "This function...", <code>grep</code>
261wouldn't help you remember the name. But because the package starts each
262doc comment with the name, you'd see something like this,
263which recalls the word you're looking for.
264</p>
265
266<pre>
267$ godoc regexp | grep parse
268 Compile parses a regular expression and returns, if successful, a Regexp
269 parsed. It simplifies safe initialization of global variables holding
270 cannot be parsed. It simplifies safe initialization of global variables
271$
272</pre>
273
274<p>
275Go's declaration syntax allows grouping of declarations.
276A single doc comment can introduce a group of related constants or variables.
277Since the whole declaration is presented, such a comment can often be perfunctory.
278</p>
279
280<pre>
281// Error codes returned by failures to parse an expression.
282var (
283 ErrInternal = errors.New("regexp: internal error")
284 ErrUnmatchedLpar = errors.New("regexp: unmatched '('")
285 ErrUnmatchedRpar = errors.New("regexp: unmatched ')'")
286 ...
287)
288</pre>
289
290<p>
291Grouping can also indicate relationships between items,
292such as the fact that a set of variables is protected by a mutex.
293</p>
294
295<pre>
296var (
297 countLock sync.Mutex
298 inputCount uint32
299 outputCount uint32
300 errorCount uint32
301)
302</pre>
303
304<h2 id="names">Names</h2>
305
306<p>
307Names are as important in Go as in any other language.
308They even have semantic effect:
309the visibility of a name outside a package is determined by whether its
310first character is upper case.
311It's therefore worth spending a little time talking about naming conventions
312in Go programs.
313</p>
314
315
316<h3 id="package-names">Package names</h3>
317
318<p>
319When a package is imported, the package name becomes an accessor for the
320contents. After
321</p>
322
323<pre>
324import "bytes"
325</pre>
326
327<p>
328the importing package can talk about <code>bytes.Buffer</code>. It's
329helpful if everyone using the package can use the same name to refer to
330its contents, which implies that the package name should be good:
331short, concise, evocative. By convention, packages are given
332lower case, single-word names; there should be no need for underscores
333or mixedCaps.
334Err on the side of brevity, since everyone using your
335package will be typing that name.
336And don't worry about collisions <i>a priori</i>.
337The package name is only the default name for imports; it need not be unique
338across all source code, and in the rare case of a collision the
339importing package can choose a different name to use locally.
340In any case, confusion is rare because the file name in the import
341determines just which package is being used.
342</p>
343
344<p>
345Another convention is that the package name is the base name of
346its source directory;
347the package in <code>src/encoding/base64</code>
348is imported as <code>"encoding/base64"</code> but has name <code>base64</code>,
349not <code>encoding_base64</code> and not <code>encodingBase64</code>.
350</p>
351
352<p>
353The importer of a package will use the name to refer to its contents,
354so exported names in the package can use that fact
355to avoid stutter.
356(Don't use the <code>import .</code> notation, which can simplify
357tests that must run outside the package they are testing, but should otherwise be avoided.)
358For instance, the buffered reader type in the <code>bufio</code> package is called <code>Reader</code>,
359not <code>BufReader</code>, because users see it as <code>bufio.Reader</code>,
360which is a clear, concise name.
361Moreover,
362because imported entities are always addressed with their package name, <code>bufio.Reader</code>
363does not conflict with <code>io.Reader</code>.
364Similarly, the function to make new instances of <code>ring.Ring</code>&mdash;which
365is the definition of a <em>constructor</em> in Go&mdash;would
366normally be called <code>NewRing</code>, but since
367<code>Ring</code> is the only type exported by the package, and since the
368package is called <code>ring</code>, it's called just <code>New</code>,
369which clients of the package see as <code>ring.New</code>.
370Use the package structure to help you choose good names.
371</p>
372
373<p>
374Another short example is <code>once.Do</code>;
375<code>once.Do(setup)</code> reads well and would not be improved by
376writing <code>once.DoOrWaitUntilDone(setup)</code>.
377Long names don't automatically make things more readable.
378A helpful doc comment can often be more valuable than an extra long name.
379</p>
380
381<h3 id="Getters">Getters</h3>
382
383<p>
384Go doesn't provide automatic support for getters and setters.
385There's nothing wrong with providing getters and setters yourself,
386and it's often appropriate to do so, but it's neither idiomatic nor necessary
387to put <code>Get</code> into the getter's name. If you have a field called
388<code>owner</code> (lower case, unexported), the getter method should be
389called <code>Owner</code> (upper case, exported), not <code>GetOwner</code>.
390The use of upper-case names for export provides the hook to discriminate
391the field from the method.
392A setter function, if needed, will likely be called <code>SetOwner</code>.
393Both names read well in practice:
394</p>
395<pre>
396owner := obj.Owner()
397if owner != user {
398 obj.SetOwner(user)
399}
400</pre>
401
402<h3 id="interface-names">Interface names</h3>
403
404<p>
405By convention, one-method interfaces are named by
406the method name plus an -er suffix or similar modification
407to construct an agent noun: <code>Reader</code>,
408<code>Writer</code>, <code>Formatter</code>,
409<code>CloseNotifier</code> etc.
410</p>
411
412<p>
413There are a number of such names and it's productive to honor them and the function
414names they capture.
415<code>Read</code>, <code>Write</code>, <code>Close</code>, <code>Flush</code>,
416<code>String</code> and so on have
417canonical signatures and meanings. To avoid confusion,
418don't give your method one of those names unless it
419has the same signature and meaning.
420Conversely, if your type implements a method with the
421same meaning as a method on a well-known type,
422give it the same name and signature;
423call your string-converter method <code>String</code> not <code>ToString</code>.
424</p>
425
426<h3 id="mixed-caps">MixedCaps</h3>
427
428<p>
429Finally, the convention in Go is to use <code>MixedCaps</code>
430or <code>mixedCaps</code> rather than underscores to write
431multiword names.
432</p>
433
434<h2 id="semicolons">Semicolons</h2>
435
436<p>
437Like C, Go's formal grammar uses semicolons to terminate statements,
438but unlike in C, those semicolons do not appear in the source.
439Instead the lexer uses a simple rule to insert semicolons automatically
440as it scans, so the input text is mostly free of them.
441</p>
442
443<p>
444The rule is this. If the last token before a newline is an identifier
445(which includes words like <code>int</code> and <code>float64</code>),
446a basic literal such as a number or string constant, or one of the
447tokens
448</p>
449<pre>
450break continue fallthrough return ++ -- ) }
451</pre>
452<p>
453the lexer always inserts a semicolon after the token.
454This could be summarized as, &ldquo;if the newline comes
455after a token that could end a statement, insert a semicolon&rdquo;.
456</p>
457
458<p>
459A semicolon can also be omitted immediately before a closing brace,
460so a statement such as
461</p>
462<pre>
463 go func() { for { dst &lt;- &lt;-src } }()
464</pre>
465<p>
466needs no semicolons.
467Idiomatic Go programs have semicolons only in places such as
468<code>for</code> loop clauses, to separate the initializer, condition, and
469continuation elements. They are also necessary to separate multiple
470statements on a line, should you write code that way.
471</p>
472
473<p>
474One consequence of the semicolon insertion rules
475is that you cannot put the opening brace of a
476control structure (<code>if</code>, <code>for</code>, <code>switch</code>,
477or <code>select</code>) on the next line. If you do, a semicolon
478will be inserted before the brace, which could cause unwanted
479effects. Write them like this
480</p>
481
482<pre>
483if i &lt; f() {
484 g()
485}
486</pre>
487<p>
488not like this
489</p>
490<pre>
491if i &lt; f() // wrong!
492{ // wrong!
493 g()
494}
495</pre>
496
497
498<h2 id="control-structures">Control structures</h2>
499
500<p>
501The control structures of Go are related to those of C but differ
502in important ways.
503There is no <code>do</code> or <code>while</code> loop, only a
504slightly generalized
505<code>for</code>;
506<code>switch</code> is more flexible;
507<code>if</code> and <code>switch</code> accept an optional
508initialization statement like that of <code>for</code>;
509<code>break</code> and <code>continue</code> statements
510take an optional label to identify what to break or continue;
511and there are new control structures including a type switch and a
512multiway communications multiplexer, <code>select</code>.
513The syntax is also slightly different:
514there are no parentheses
515and the bodies must always be brace-delimited.
516</p>
517
518<h3 id="if">If</h3>
519
520<p>
521In Go a simple <code>if</code> looks like this:
522</p>
523<pre>
524if x &gt; 0 {
525 return y
526}
527</pre>
528
529<p>
530Mandatory braces encourage writing simple <code>if</code> statements
531on multiple lines. It's good style to do so anyway,
532especially when the body contains a control statement such as a
533<code>return</code> or <code>break</code>.
534</p>
535
536<p>
537Since <code>if</code> and <code>switch</code> accept an initialization
538statement, it's common to see one used to set up a local variable.
539</p>
540
541<pre>
542if err := file.Chmod(0664); err != nil {
543 log.Print(err)
544 return err
545}
546</pre>
547
548<p id="else">
549In the Go libraries, you'll find that
550when an <code>if</code> statement doesn't flow into the next statement—that is,
551the body ends in <code>break</code>, <code>continue</code>,
552<code>goto</code>, or <code>return</code>—the unnecessary
553<code>else</code> is omitted.
554</p>
555
556<pre>
557f, err := os.Open(name)
558if err != nil {
559 return err
560}
561codeUsing(f)
562</pre>
563
564<p>
565This is an example of a common situation where code must guard against a
566sequence of error conditions. The code reads well if the
567successful flow of control runs down the page, eliminating error cases
568as they arise. Since error cases tend to end in <code>return</code>
569statements, the resulting code needs no <code>else</code> statements.
570</p>
571
572<pre>
573f, err := os.Open(name)
574if err != nil {
575 return err
576}
577d, err := f.Stat()
578if err != nil {
579 f.Close()
580 return err
581}
582codeUsing(f, d)
583</pre>
584
585
586<h3 id="redeclaration">Redeclaration and reassignment</h3>
587
588<p>
589An aside: The last example in the previous section demonstrates a detail of how the
590<code>:=</code> short declaration form works.
591The declaration that calls <code>os.Open</code> reads,
592</p>
593
594<pre>
595f, err := os.Open(name)
596</pre>
597
598<p>
599This statement declares two variables, <code>f</code> and <code>err</code>.
600A few lines later, the call to <code>f.Stat</code> reads,
601</p>
602
603<pre>
604d, err := f.Stat()
605</pre>
606
607<p>
608which looks as if it declares <code>d</code> and <code>err</code>.
609Notice, though, that <code>err</code> appears in both statements.
610This duplication is legal: <code>err</code> is declared by the first statement,
611but only <em>re-assigned</em> in the second.
612This means that the call to <code>f.Stat</code> uses the existing
613<code>err</code> variable declared above, and just gives it a new value.
614</p>
615
616<p>
617In a <code>:=</code> declaration a variable <code>v</code> may appear even
618if it has already been declared, provided:
619</p>
620
621<ul>
622<li>this declaration is in the same scope as the existing declaration of <code>v</code>
623(if <code>v</code> is already declared in an outer scope, the declaration will create a new variable §),</li>
624<li>the corresponding value in the initialization is assignable to <code>v</code>, and</li>
625<li>there is at least one other variable in the declaration that is being declared anew.</li>
626</ul>
627
628<p>
629This unusual property is pure pragmatism,
630making it easy to use a single <code>err</code> value, for example,
631in a long <code>if-else</code> chain.
632You'll see it used often.
633</p>
634
635<p>
636§ It's worth noting here that in Go the scope of function parameters and return values
637is the same as the function body, even though they appear lexically outside the braces
638that enclose the body.
639</p>
640
641<h3 id="for">For</h3>
642
643<p>
644The Go <code>for</code> loop is similar to&mdash;but not the same as&mdash;C's.
645It unifies <code>for</code>
646and <code>while</code> and there is no <code>do-while</code>.
647There are three forms, only one of which has semicolons.
648</p>
649<pre>
650// Like a C for
651for init; condition; post { }
652
653// Like a C while
654for condition { }
655
656// Like a C for(;;)
657for { }
658</pre>
659
660<p>
661Short declarations make it easy to declare the index variable right in the loop.
662</p>
663<pre>
664sum := 0
665for i := 0; i &lt; 10; i++ {
666 sum += i
667}
668</pre>
669
670<p>
671If you're looping over an array, slice, string, or map,
672or reading from a channel, a <code>range</code> clause can
673manage the loop.
674</p>
675<pre>
676for key, value := range oldMap {
677 newMap[key] = value
678}
679</pre>
680
681<p>
682If you only need the first item in the range (the key or index), drop the second:
683</p>
684<pre>
685for key := range m {
686 if key.expired() {
687 delete(m, key)
688 }
689}
690</pre>
691
692<p>
693If you only need the second item in the range (the value), use the <em>blank identifier</em>, an underscore, to discard the first:
694</p>
695<pre>
696sum := 0
697for _, value := range array {
698 sum += value
699}
700</pre>
701
702<p>
703The blank identifier has many uses, as described in <a href="#blank">a later section</a>.
704</p>
705
706<p>
707For strings, the <code>range</code> does more work for you, breaking out individual
708Unicode code points by parsing the UTF-8.
709Erroneous encodings consume one byte and produce the
710replacement rune U+FFFD.
711(The name (with associated builtin type) <code>rune</code> is Go terminology for a
712single Unicode code point.
713See <a href="/ref/spec#Rune_literals">the language specification</a>
714for details.)
715The loop
716</p>
717<pre>
718for pos, char := range "日本\x80語" { // \x80 is an illegal UTF-8 encoding
719 fmt.Printf("character %#U starts at byte position %d\n", char, pos)
720}
721</pre>
722<p>
723prints
724</p>
725<pre>
726character U+65E5 '日' starts at byte position 0
727character U+672C '本' starts at byte position 3
728character U+FFFD '�' starts at byte position 6
729character U+8A9E '語' starts at byte position 7
730</pre>
731
732<p>
733Finally, Go has no comma operator and <code>++</code> and <code>--</code>
734are statements not expressions.
735Thus if you want to run multiple variables in a <code>for</code>
736you should use parallel assignment (although that precludes <code>++</code> and <code>--</code>).
737</p>
738<pre>
739// Reverse a
740for i, j := 0, len(a)-1; i &lt; j; i, j = i+1, j-1 {
741 a[i], a[j] = a[j], a[i]
742}
743</pre>
744
745<h3 id="switch">Switch</h3>
746
747<p>
748Go's <code>switch</code> is more general than C's.
749The expressions need not be constants or even integers,
750the cases are evaluated top to bottom until a match is found,
751and if the <code>switch</code> has no expression it switches on
752<code>true</code>.
753It's therefore possible&mdash;and idiomatic&mdash;to write an
754<code>if</code>-<code>else</code>-<code>if</code>-<code>else</code>
755chain as a <code>switch</code>.
756</p>
757
758<pre>
759func unhex(c byte) byte {
760 switch {
761 case '0' &lt;= c &amp;&amp; c &lt;= '9':
762 return c - '0'
763 case 'a' &lt;= c &amp;&amp; c &lt;= 'f':
764 return c - 'a' + 10
765 case 'A' &lt;= c &amp;&amp; c &lt;= 'F':
766 return c - 'A' + 10
767 }
768 return 0
769}
770</pre>
771
772<p>
773There is no automatic fall through, but cases can be presented
774in comma-separated lists.
775</p>
776<pre>
777func shouldEscape(c byte) bool {
778 switch c {
779 case ' ', '?', '&amp;', '=', '#', '+', '%':
780 return true
781 }
782 return false
783}
784</pre>
785
786<p>
787Although they are not nearly as common in Go as some other C-like
788languages, <code>break</code> statements can be used to terminate
789a <code>switch</code> early.
790Sometimes, though, it's necessary to break out of a surrounding loop,
791not the switch, and in Go that can be accomplished by putting a label
792on the loop and "breaking" to that label.
793This example shows both uses.
794</p>
795
796<pre>
797Loop:
798 for n := 0; n &lt; len(src); n += size {
799 switch {
800 case src[n] &lt; sizeOne:
801 if validateOnly {
802 break
803 }
804 size = 1
805 update(src[n])
806
807 case src[n] &lt; sizeTwo:
808 if n+1 &gt;= len(src) {
809 err = errShortInput
810 break Loop
811 }
812 if validateOnly {
813 break
814 }
815 size = 2
816 update(src[n] + src[n+1]&lt;&lt;shift)
817 }
818 }
819</pre>
820
821<p>
822Of course, the <code>continue</code> statement also accepts an optional label
823but it applies only to loops.
824</p>
825
826<p>
827To close this section, here's a comparison routine for byte slices that uses two
828<code>switch</code> statements:
829</p>
830<pre>
831// Compare returns an integer comparing the two byte slices,
832// lexicographically.
833// The result will be 0 if a == b, -1 if a &lt; b, and +1 if a &gt; b
834func Compare(a, b []byte) int {
835 for i := 0; i &lt; len(a) &amp;&amp; i &lt; len(b); i++ {
836 switch {
837 case a[i] &gt; b[i]:
838 return 1
839 case a[i] &lt; b[i]:
840 return -1
841 }
842 }
843 switch {
844 case len(a) &gt; len(b):
845 return 1
846 case len(a) &lt; len(b):
847 return -1
848 }
849 return 0
850}
851</pre>
852
853<h3 id="type_switch">Type switch</h3>
854
855<p>
856A switch can also be used to discover the dynamic type of an interface
857variable. Such a <em>type switch</em> uses the syntax of a type
858assertion with the keyword <code>type</code> inside the parentheses.
859If the switch declares a variable in the expression, the variable will
860have the corresponding type in each clause.
861It's also idiomatic to reuse the name in such cases, in effect declaring
862a new variable with the same name but a different type in each case.
863</p>
864<pre>
865var t interface{}
866t = functionOfSomeType()
867switch t := t.(type) {
868default:
Dan Willemsen09eb3b12015-09-16 14:34:17 -0700869 fmt.Printf("unexpected type %T\n", t) // %T prints whatever type t has
Brent Austinba3052e2015-04-21 16:08:23 -0700870case bool:
871 fmt.Printf("boolean %t\n", t) // t has type bool
872case int:
873 fmt.Printf("integer %d\n", t) // t has type int
874case *bool:
875 fmt.Printf("pointer to boolean %t\n", *t) // t has type *bool
876case *int:
877 fmt.Printf("pointer to integer %d\n", *t) // t has type *int
878}
879</pre>
880
881<h2 id="functions">Functions</h2>
882
883<h3 id="multiple-returns">Multiple return values</h3>
884
885<p>
886One of Go's unusual features is that functions and methods
887can return multiple values. This form can be used to
888improve on a couple of clumsy idioms in C programs: in-band
889error returns such as <code>-1</code> for <code>EOF</code>
890and modifying an argument passed by address.
891</p>
892
893<p>
894In C, a write error is signaled by a negative count with the
895error code secreted away in a volatile location.
896In Go, <code>Write</code>
897can return a count <i>and</i> an error: &ldquo;Yes, you wrote some
898bytes but not all of them because you filled the device&rdquo;.
899The signature of the <code>Write</code> method on files from
900package <code>os</code> is:
901</p>
902
903<pre>
904func (file *File) Write(b []byte) (n int, err error)
905</pre>
906
907<p>
908and as the documentation says, it returns the number of bytes
909written and a non-nil <code>error</code> when <code>n</code>
910<code>!=</code> <code>len(b)</code>.
911This is a common style; see the section on error handling for more examples.
912</p>
913
914<p>
915A similar approach obviates the need to pass a pointer to a return
916value to simulate a reference parameter.
917Here's a simple-minded function to
918grab a number from a position in a byte slice, returning the number
919and the next position.
920</p>
921
922<pre>
923func nextInt(b []byte, i int) (int, int) {
924 for ; i &lt; len(b) &amp;&amp; !isDigit(b[i]); i++ {
925 }
926 x := 0
927 for ; i &lt; len(b) &amp;&amp; isDigit(b[i]); i++ {
928 x = x*10 + int(b[i]) - '0'
929 }
930 return x, i
931}
932</pre>
933
934<p>
935You could use it to scan the numbers in an input slice <code>b</code> like this:
936</p>
937
938<pre>
939 for i := 0; i &lt; len(b); {
940 x, i = nextInt(b, i)
941 fmt.Println(x)
942 }
943</pre>
944
945<h3 id="named-results">Named result parameters</h3>
946
947<p>
948The return or result "parameters" of a Go function can be given names and
949used as regular variables, just like the incoming parameters.
950When named, they are initialized to the zero values for their types when
951the function begins; if the function executes a <code>return</code> statement
952with no arguments, the current values of the result parameters are
953used as the returned values.
954</p>
955
956<p>
957The names are not mandatory but they can make code shorter and clearer:
958they're documentation.
959If we name the results of <code>nextInt</code> it becomes
960obvious which returned <code>int</code>
961is which.
962</p>
963
964<pre>
965func nextInt(b []byte, pos int) (value, nextPos int) {
966</pre>
967
968<p>
969Because named results are initialized and tied to an unadorned return, they can simplify
970as well as clarify. Here's a version
971of <code>io.ReadFull</code> that uses them well:
972</p>
973
974<pre>
975func ReadFull(r Reader, buf []byte) (n int, err error) {
976 for len(buf) &gt; 0 &amp;&amp; err == nil {
977 var nr int
978 nr, err = r.Read(buf)
979 n += nr
980 buf = buf[nr:]
981 }
982 return
983}
984</pre>
985
986<h3 id="defer">Defer</h3>
987
988<p>
989Go's <code>defer</code> statement schedules a function call (the
990<i>deferred</i> function) to be run immediately before the function
991executing the <code>defer</code> returns. It's an unusual but
992effective way to deal with situations such as resources that must be
993released regardless of which path a function takes to return. The
994canonical examples are unlocking a mutex or closing a file.
995</p>
996
997<pre>
998// Contents returns the file's contents as a string.
999func Contents(filename string) (string, error) {
1000 f, err := os.Open(filename)
1001 if err != nil {
1002 return "", err
1003 }
1004 defer f.Close() // f.Close will run when we're finished.
1005
1006 var result []byte
1007 buf := make([]byte, 100)
1008 for {
1009 n, err := f.Read(buf[0:])
1010 result = append(result, buf[0:n]...) // append is discussed later.
1011 if err != nil {
1012 if err == io.EOF {
1013 break
1014 }
1015 return "", err // f will be closed if we return here.
1016 }
1017 }
1018 return string(result), nil // f will be closed if we return here.
1019}
1020</pre>
1021
1022<p>
1023Deferring a call to a function such as <code>Close</code> has two advantages. First, it
1024guarantees that you will never forget to close the file, a mistake
1025that's easy to make if you later edit the function to add a new return
1026path. Second, it means that the close sits near the open,
1027which is much clearer than placing it at the end of the function.
1028</p>
1029
1030<p>
1031The arguments to the deferred function (which include the receiver if
1032the function is a method) are evaluated when the <i>defer</i>
1033executes, not when the <i>call</i> executes. Besides avoiding worries
1034about variables changing values as the function executes, this means
1035that a single deferred call site can defer multiple function
1036executions. Here's a silly example.
1037</p>
1038
1039<pre>
1040for i := 0; i &lt; 5; i++ {
1041 defer fmt.Printf("%d ", i)
1042}
1043</pre>
1044
1045<p>
1046Deferred functions are executed in LIFO order, so this code will cause
1047<code>4 3 2 1 0</code> to be printed when the function returns. A
1048more plausible example is a simple way to trace function execution
1049through the program. We could write a couple of simple tracing
1050routines like this:
1051</p>
1052
1053<pre>
1054func trace(s string) { fmt.Println("entering:", s) }
1055func untrace(s string) { fmt.Println("leaving:", s) }
1056
1057// Use them like this:
1058func a() {
1059 trace("a")
1060 defer untrace("a")
1061 // do something....
1062}
1063</pre>
1064
1065<p>
1066We can do better by exploiting the fact that arguments to deferred
1067functions are evaluated when the <code>defer</code> executes. The
1068tracing routine can set up the argument to the untracing routine.
1069This example:
1070</p>
1071
1072<pre>
1073func trace(s string) string {
1074 fmt.Println("entering:", s)
1075 return s
1076}
1077
1078func un(s string) {
1079 fmt.Println("leaving:", s)
1080}
1081
1082func a() {
1083 defer un(trace("a"))
1084 fmt.Println("in a")
1085}
1086
1087func b() {
1088 defer un(trace("b"))
1089 fmt.Println("in b")
1090 a()
1091}
1092
1093func main() {
1094 b()
1095}
1096</pre>
1097
1098<p>
1099prints
1100</p>
1101
1102<pre>
1103entering: b
1104in b
1105entering: a
1106in a
1107leaving: a
1108leaving: b
1109</pre>
1110
1111<p>
1112For programmers accustomed to block-level resource management from
1113other languages, <code>defer</code> may seem peculiar, but its most
1114interesting and powerful applications come precisely from the fact
1115that it's not block-based but function-based. In the section on
1116<code>panic</code> and <code>recover</code> we'll see another
1117example of its possibilities.
1118</p>
1119
1120<h2 id="data">Data</h2>
1121
1122<h3 id="allocation_new">Allocation with <code>new</code></h3>
1123
1124<p>
1125Go has two allocation primitives, the built-in functions
1126<code>new</code> and <code>make</code>.
1127They do different things and apply to different types, which can be confusing,
1128but the rules are simple.
1129Let's talk about <code>new</code> first.
1130It's a built-in function that allocates memory, but unlike its namesakes
1131in some other languages it does not <em>initialize</em> the memory,
1132it only <em>zeros</em> it.
1133That is,
1134<code>new(T)</code> allocates zeroed storage for a new item of type
1135<code>T</code> and returns its address, a value of type <code>*T</code>.
1136In Go terminology, it returns a pointer to a newly allocated zero value of type
1137<code>T</code>.
1138</p>
1139
1140<p>
1141Since the memory returned by <code>new</code> is zeroed, it's helpful to arrange
1142when designing your data structures that the
1143zero value of each type can be used without further initialization. This means a user of
1144the data structure can create one with <code>new</code> and get right to
1145work.
1146For example, the documentation for <code>bytes.Buffer</code> states that
1147"the zero value for <code>Buffer</code> is an empty buffer ready to use."
1148Similarly, <code>sync.Mutex</code> does not
1149have an explicit constructor or <code>Init</code> method.
1150Instead, the zero value for a <code>sync.Mutex</code>
1151is defined to be an unlocked mutex.
1152</p>
1153
1154<p>
1155The zero-value-is-useful property works transitively. Consider this type declaration.
1156</p>
1157
1158<pre>
1159type SyncedBuffer struct {
1160 lock sync.Mutex
1161 buffer bytes.Buffer
1162}
1163</pre>
1164
1165<p>
1166Values of type <code>SyncedBuffer</code> are also ready to use immediately upon allocation
1167or just declaration. In the next snippet, both <code>p</code> and <code>v</code> will work
1168correctly without further arrangement.
1169</p>
1170
1171<pre>
1172p := new(SyncedBuffer) // type *SyncedBuffer
1173var v SyncedBuffer // type SyncedBuffer
1174</pre>
1175
1176<h3 id="composite_literals">Constructors and composite literals</h3>
1177
1178<p>
1179Sometimes the zero value isn't good enough and an initializing
1180constructor is necessary, as in this example derived from
1181package <code>os</code>.
1182</p>
1183
1184<pre>
1185func NewFile(fd int, name string) *File {
1186 if fd &lt; 0 {
1187 return nil
1188 }
1189 f := new(File)
1190 f.fd = fd
1191 f.name = name
1192 f.dirinfo = nil
1193 f.nepipe = 0
1194 return f
1195}
1196</pre>
1197
1198<p>
1199There's a lot of boiler plate in there. We can simplify it
1200using a <i>composite literal</i>, which is
1201an expression that creates a
1202new instance each time it is evaluated.
1203</p>
1204
1205<pre>
1206func NewFile(fd int, name string) *File {
1207 if fd &lt; 0 {
1208 return nil
1209 }
1210 f := File{fd, name, nil, 0}
1211 return &amp;f
1212}
1213</pre>
1214
1215<p>
1216Note that, unlike in C, it's perfectly OK to return the address of a local variable;
1217the storage associated with the variable survives after the function
1218returns.
1219In fact, taking the address of a composite literal
1220allocates a fresh instance each time it is evaluated,
1221so we can combine these last two lines.
1222</p>
1223
1224<pre>
1225 return &amp;File{fd, name, nil, 0}
1226</pre>
1227
1228<p>
1229The fields of a composite literal are laid out in order and must all be present.
1230However, by labeling the elements explicitly as <i>field</i><code>:</code><i>value</i>
1231pairs, the initializers can appear in any
1232order, with the missing ones left as their respective zero values. Thus we could say
1233</p>
1234
1235<pre>
1236 return &amp;File{fd: fd, name: name}
1237</pre>
1238
1239<p>
1240As a limiting case, if a composite literal contains no fields at all, it creates
1241a zero value for the type. The expressions <code>new(File)</code> and <code>&amp;File{}</code> are equivalent.
1242</p>
1243
1244<p>
1245Composite literals can also be created for arrays, slices, and maps,
1246with the field labels being indices or map keys as appropriate.
1247In these examples, the initializations work regardless of the values of <code>Enone</code>,
1248<code>Eio</code>, and <code>Einval</code>, as long as they are distinct.
1249</p>
1250
1251<pre>
1252a := [...]string {Enone: "no error", Eio: "Eio", Einval: "invalid argument"}
1253s := []string {Enone: "no error", Eio: "Eio", Einval: "invalid argument"}
1254m := map[int]string{Enone: "no error", Eio: "Eio", Einval: "invalid argument"}
1255</pre>
1256
1257<h3 id="allocation_make">Allocation with <code>make</code></h3>
1258
1259<p>
1260Back to allocation.
1261The built-in function <code>make(T, </code><i>args</i><code>)</code> serves
1262a purpose different from <code>new(T)</code>.
1263It creates slices, maps, and channels only, and it returns an <em>initialized</em>
1264(not <em>zeroed</em>)
1265value of type <code>T</code> (not <code>*T</code>).
1266The reason for the distinction
1267is that these three types represent, under the covers, references to data structures that
1268must be initialized before use.
1269A slice, for example, is a three-item descriptor
1270containing a pointer to the data (inside an array), the length, and the
1271capacity, and until those items are initialized, the slice is <code>nil</code>.
1272For slices, maps, and channels,
1273<code>make</code> initializes the internal data structure and prepares
1274the value for use.
1275For instance,
1276</p>
1277
1278<pre>
1279make([]int, 10, 100)
1280</pre>
1281
1282<p>
1283allocates an array of 100 ints and then creates a slice
1284structure with length 10 and a capacity of 100 pointing at the first
128510 elements of the array.
1286(When making a slice, the capacity can be omitted; see the section on slices
1287for more information.)
1288In contrast, <code>new([]int)</code> returns a pointer to a newly allocated, zeroed slice
1289structure, that is, a pointer to a <code>nil</code> slice value.
1290</p>
1291
1292<p>
1293These examples illustrate the difference between <code>new</code> and
1294<code>make</code>.
1295</p>
1296
1297<pre>
1298var p *[]int = new([]int) // allocates slice structure; *p == nil; rarely useful
1299var v []int = make([]int, 100) // the slice v now refers to a new array of 100 ints
1300
1301// Unnecessarily complex:
1302var p *[]int = new([]int)
1303*p = make([]int, 100, 100)
1304
1305// Idiomatic:
1306v := make([]int, 100)
1307</pre>
1308
1309<p>
1310Remember that <code>make</code> applies only to maps, slices and channels
1311and does not return a pointer.
1312To obtain an explicit pointer allocate with <code>new</code> or take the address
1313of a variable explicitly.
1314</p>
1315
1316<h3 id="arrays">Arrays</h3>
1317
1318<p>
1319Arrays are useful when planning the detailed layout of memory and sometimes
1320can help avoid allocation, but primarily
1321they are a building block for slices, the subject of the next section.
1322To lay the foundation for that topic, here are a few words about arrays.
1323</p>
1324
1325<p>
1326There are major differences between the ways arrays work in Go and C.
1327In Go,
1328</p>
1329<ul>
1330<li>
1331Arrays are values. Assigning one array to another copies all the elements.
1332</li>
1333<li>
1334In particular, if you pass an array to a function, it
1335will receive a <i>copy</i> of the array, not a pointer to it.
1336<li>
1337The size of an array is part of its type. The types <code>[10]int</code>
1338and <code>[20]int</code> are distinct.
1339</li>
1340</ul>
1341
1342<p>
1343The value property can be useful but also expensive; if you want C-like behavior and efficiency,
1344you can pass a pointer to the array.
1345</p>
1346
1347<pre>
1348func Sum(a *[3]float64) (sum float64) {
1349 for _, v := range *a {
1350 sum += v
1351 }
1352 return
1353}
1354
1355array := [...]float64{7.0, 8.5, 9.1}
1356x := Sum(&amp;array) // Note the explicit address-of operator
1357</pre>
1358
1359<p>
1360But even this style isn't idiomatic Go.
1361Use slices instead.
1362</p>
1363
1364<h3 id="slices">Slices</h3>
1365
1366<p>
1367Slices wrap arrays to give a more general, powerful, and convenient
1368interface to sequences of data. Except for items with explicit
1369dimension such as transformation matrices, most array programming in
1370Go is done with slices rather than simple arrays.
1371</p>
1372<p>
1373Slices hold references to an underlying array, and if you assign one
1374slice to another, both refer to the same array.
1375If a function takes a slice argument, changes it makes to
1376the elements of the slice will be visible to the caller, analogous to
1377passing a pointer to the underlying array. A <code>Read</code>
1378function can therefore accept a slice argument rather than a pointer
1379and a count; the length within the slice sets an upper
1380limit of how much data to read. Here is the signature of the
1381<code>Read</code> method of the <code>File</code> type in package
1382<code>os</code>:
1383</p>
1384<pre>
Dan Willemsen09eb3b12015-09-16 14:34:17 -07001385func (f *File) Read(buf []byte) (n int, err error)
Brent Austinba3052e2015-04-21 16:08:23 -07001386</pre>
1387<p>
1388The method returns the number of bytes read and an error value, if
1389any.
1390To read into the first 32 bytes of a larger buffer
1391<code>buf</code>, <i>slice</i> (here used as a verb) the buffer.
1392</p>
1393<pre>
1394 n, err := f.Read(buf[0:32])
1395</pre>
1396<p>
1397Such slicing is common and efficient. In fact, leaving efficiency aside for
1398the moment, the following snippet would also read the first 32 bytes of the buffer.
1399</p>
1400<pre>
1401 var n int
1402 var err error
1403 for i := 0; i &lt; 32; i++ {
1404 nbytes, e := f.Read(buf[i:i+1]) // Read one byte.
1405 if nbytes == 0 || e != nil {
1406 err = e
1407 break
1408 }
1409 n += nbytes
1410 }
1411</pre>
1412<p>
1413The length of a slice may be changed as long as it still fits within
1414the limits of the underlying array; just assign it to a slice of
1415itself. The <i>capacity</i> of a slice, accessible by the built-in
1416function <code>cap</code>, reports the maximum length the slice may
1417assume. Here is a function to append data to a slice. If the data
1418exceeds the capacity, the slice is reallocated. The
1419resulting slice is returned. The function uses the fact that
1420<code>len</code> and <code>cap</code> are legal when applied to the
1421<code>nil</code> slice, and return 0.
1422</p>
1423<pre>
Dan Willemsen09eb3b12015-09-16 14:34:17 -07001424func Append(slice, data []byte) []byte {
Brent Austinba3052e2015-04-21 16:08:23 -07001425 l := len(slice)
1426 if l + len(data) &gt; cap(slice) { // reallocate
1427 // Allocate double what's needed, for future growth.
1428 newSlice := make([]byte, (l+len(data))*2)
1429 // The copy function is predeclared and works for any slice type.
1430 copy(newSlice, slice)
1431 slice = newSlice
1432 }
1433 slice = slice[0:l+len(data)]
1434 for i, c := range data {
1435 slice[l+i] = c
1436 }
1437 return slice
1438}
1439</pre>
1440<p>
1441We must return the slice afterwards because, although <code>Append</code>
1442can modify the elements of <code>slice</code>, the slice itself (the run-time data
1443structure holding the pointer, length, and capacity) is passed by value.
1444</p>
1445
1446<p>
1447The idea of appending to a slice is so useful it's captured by the
1448<code>append</code> built-in function. To understand that function's
1449design, though, we need a little more information, so we'll return
1450to it later.
1451</p>
1452
1453<h3 id="two_dimensional_slices">Two-dimensional slices</h3>
1454
1455<p>
1456Go's arrays and slices are one-dimensional.
1457To create the equivalent of a 2D array or slice, it is necessary to define an array-of-arrays
1458or slice-of-slices, like this:
1459</p>
1460
1461<pre>
1462type Transform [3][3]float64 // A 3x3 array, really an array of arrays.
1463type LinesOfText [][]byte // A slice of byte slices.
1464</pre>
1465
1466<p>
1467Because slices are variable-length, it is possible to have each inner
1468slice be a different length.
1469That can be a common situation, as in our <code>LinesOfText</code>
1470example: each line has an independent length.
1471</p>
1472
1473<pre>
1474text := LinesOfText{
1475 []byte("Now is the time"),
1476 []byte("for all good gophers"),
1477 []byte("to bring some fun to the party."),
1478}
1479</pre>
1480
1481<p>
1482Sometimes it's necessary to allocate a 2D slice, a situation that can arise when
1483processing scan lines of pixels, for instance.
1484There are two ways to achieve this.
1485One is to allocate each slice independently; the other
1486is to allocate a single array and point the individual slices into it.
1487Which to use depends on your application.
1488If the slices might grow or shrink, they should be allocated independently
1489to avoid overwriting the next line; if not, it can be more efficient to construct
1490the object with a single allocation.
1491For reference, here are sketches of the two methods.
1492First, a line at a time:
1493</p>
1494
1495<pre>
1496// Allocate the top-level slice.
1497picture := make([][]uint8, YSize) // One row per unit of y.
1498// Loop over the rows, allocating the slice for each row.
1499for i := range picture {
1500 picture[i] = make([]uint8, XSize)
1501}
1502</pre>
1503
1504<p>
1505And now as one allocation, sliced into lines:
1506</p>
1507
1508<pre>
1509// Allocate the top-level slice, the same as before.
1510picture := make([][]uint8, YSize) // One row per unit of y.
1511// Allocate one large slice to hold all the pixels.
1512pixels := make([]uint8, XSize*YSize) // Has type []uint8 even though picture is [][]uint8.
1513// Loop over the rows, slicing each row from the front of the remaining pixels slice.
1514for i := range picture {
1515 picture[i], pixels = pixels[:XSize], pixels[XSize:]
1516}
1517</pre>
1518
1519<h3 id="maps">Maps</h3>
1520
1521<p>
1522Maps are a convenient and powerful built-in data structure that associate
1523values of one type (the <em>key</em>) with values of another type
1524(the <em>element</em> or <em>value</em>)
1525The key can be of any type for which the equality operator is defined,
1526such as integers,
1527floating point and complex numbers,
1528strings, pointers, interfaces (as long as the dynamic type
1529supports equality), structs and arrays.
1530Slices cannot be used as map keys,
1531because equality is not defined on them.
1532Like slices, maps hold references to an underlying data structure.
1533If you pass a map to a function
1534that changes the contents of the map, the changes will be visible
1535in the caller.
1536</p>
1537<p>
1538Maps can be constructed using the usual composite literal syntax
1539with colon-separated key-value pairs,
1540so it's easy to build them during initialization.
1541</p>
1542<pre>
1543var timeZone = map[string]int{
1544 "UTC": 0*60*60,
1545 "EST": -5*60*60,
1546 "CST": -6*60*60,
1547 "MST": -7*60*60,
1548 "PST": -8*60*60,
1549}
1550</pre>
1551<p>
1552Assigning and fetching map values looks syntactically just like
1553doing the same for arrays and slices except that the index doesn't
1554need to be an integer.
1555</p>
1556<pre>
1557offset := timeZone["EST"]
1558</pre>
1559<p>
1560An attempt to fetch a map value with a key that
1561is not present in the map will return the zero value for the type
1562of the entries
1563in the map. For instance, if the map contains integers, looking
1564up a non-existent key will return <code>0</code>.
1565A set can be implemented as a map with value type <code>bool</code>.
1566Set the map entry to <code>true</code> to put the value in the set, and then
1567test it by simple indexing.
1568</p>
1569<pre>
1570attended := map[string]bool{
1571 "Ann": true,
1572 "Joe": true,
1573 ...
1574}
1575
1576if attended[person] { // will be false if person is not in the map
1577 fmt.Println(person, "was at the meeting")
1578}
1579</pre>
1580<p>
1581Sometimes you need to distinguish a missing entry from
1582a zero value. Is there an entry for <code>"UTC"</code>
1583or is that the empty string because it's not in the map at all?
1584You can discriminate with a form of multiple assignment.
1585</p>
1586<pre>
1587var seconds int
1588var ok bool
1589seconds, ok = timeZone[tz]
1590</pre>
1591<p>
1592For obvious reasons this is called the &ldquo;comma ok&rdquo; idiom.
1593In this example, if <code>tz</code> is present, <code>seconds</code>
1594will be set appropriately and <code>ok</code> will be true; if not,
1595<code>seconds</code> will be set to zero and <code>ok</code> will
1596be false.
1597Here's a function that puts it together with a nice error report:
1598</p>
1599<pre>
1600func offset(tz string) int {
1601 if seconds, ok := timeZone[tz]; ok {
1602 return seconds
1603 }
1604 log.Println("unknown time zone:", tz)
1605 return 0
1606}
1607</pre>
1608<p>
1609To test for presence in the map without worrying about the actual value,
1610you can use the <a href="#blank">blank identifier</a> (<code>_</code>)
1611in place of the usual variable for the value.
1612</p>
1613<pre>
1614_, present := timeZone[tz]
1615</pre>
1616<p>
1617To delete a map entry, use the <code>delete</code>
1618built-in function, whose arguments are the map and the key to be deleted.
1619It's safe to do this even if the key is already absent
1620from the map.
1621</p>
1622<pre>
1623delete(timeZone, "PDT") // Now on Standard Time
1624</pre>
1625
1626<h3 id="printing">Printing</h3>
1627
1628<p>
1629Formatted printing in Go uses a style similar to C's <code>printf</code>
1630family but is richer and more general. The functions live in the <code>fmt</code>
1631package and have capitalized names: <code>fmt.Printf</code>, <code>fmt.Fprintf</code>,
1632<code>fmt.Sprintf</code> and so on. The string functions (<code>Sprintf</code> etc.)
1633return a string rather than filling in a provided buffer.
1634</p>
1635<p>
1636You don't need to provide a format string. For each of <code>Printf</code>,
1637<code>Fprintf</code> and <code>Sprintf</code> there is another pair
1638of functions, for instance <code>Print</code> and <code>Println</code>.
1639These functions do not take a format string but instead generate a default
1640format for each argument. The <code>Println</code> versions also insert a blank
1641between arguments and append a newline to the output while
1642the <code>Print</code> versions add blanks only if the operand on neither side is a string.
1643In this example each line produces the same output.
1644</p>
1645<pre>
1646fmt.Printf("Hello %d\n", 23)
1647fmt.Fprint(os.Stdout, "Hello ", 23, "\n")
1648fmt.Println("Hello", 23)
1649fmt.Println(fmt.Sprint("Hello ", 23))
1650</pre>
1651<p>
1652The formatted print functions <code>fmt.Fprint</code>
1653and friends take as a first argument any object
1654that implements the <code>io.Writer</code> interface; the variables <code>os.Stdout</code>
1655and <code>os.Stderr</code> are familiar instances.
1656</p>
1657<p>
1658Here things start to diverge from C. First, the numeric formats such as <code>%d</code>
1659do not take flags for signedness or size; instead, the printing routines use the
1660type of the argument to decide these properties.
1661</p>
1662<pre>
1663var x uint64 = 1&lt;&lt;64 - 1
1664fmt.Printf("%d %x; %d %x\n", x, x, int64(x), int64(x))
1665</pre>
1666<p>
1667prints
1668</p>
1669<pre>
167018446744073709551615 ffffffffffffffff; -1 -1
1671</pre>
1672<p>
1673If you just want the default conversion, such as decimal for integers, you can use
1674the catchall format <code>%v</code> (for &ldquo;value&rdquo;); the result is exactly
1675what <code>Print</code> and <code>Println</code> would produce.
1676Moreover, that format can print <em>any</em> value, even arrays, slices, structs, and
1677maps. Here is a print statement for the time zone map defined in the previous section.
1678</p>
1679<pre>
1680fmt.Printf("%v\n", timeZone) // or just fmt.Println(timeZone)
1681</pre>
1682<p>
1683which gives output
1684</p>
1685<pre>
1686map[CST:-21600 PST:-28800 EST:-18000 UTC:0 MST:-25200]
1687</pre>
1688<p>
1689For maps the keys may be output in any order, of course.
1690When printing a struct, the modified format <code>%+v</code> annotates the
1691fields of the structure with their names, and for any value the alternate
1692format <code>%#v</code> prints the value in full Go syntax.
1693</p>
1694<pre>
1695type T struct {
1696 a int
1697 b float64
1698 c string
1699}
1700t := &amp;T{ 7, -2.35, "abc\tdef" }
1701fmt.Printf("%v\n", t)
1702fmt.Printf("%+v\n", t)
1703fmt.Printf("%#v\n", t)
1704fmt.Printf("%#v\n", timeZone)
1705</pre>
1706<p>
1707prints
1708</p>
1709<pre>
1710&amp;{7 -2.35 abc def}
1711&amp;{a:7 b:-2.35 c:abc def}
1712&amp;main.T{a:7, b:-2.35, c:"abc\tdef"}
1713map[string] int{"CST":-21600, "PST":-28800, "EST":-18000, "UTC":0, "MST":-25200}
1714</pre>
1715<p>
1716(Note the ampersands.)
1717That quoted string format is also available through <code>%q</code> when
1718applied to a value of type <code>string</code> or <code>[]byte</code>.
1719The alternate format <code>%#q</code> will use backquotes instead if possible.
1720(The <code>%q</code> format also applies to integers and runes, producing a
1721single-quoted rune constant.)
1722Also, <code>%x</code> works on strings, byte arrays and byte slices as well as
1723on integers, generating a long hexadecimal string, and with
1724a space in the format (<code>%&nbsp;x</code>) it puts spaces between the bytes.
1725</p>
1726<p>
1727Another handy format is <code>%T</code>, which prints the <em>type</em> of a value.
1728</p>
1729<pre>
1730fmt.Printf(&quot;%T\n&quot;, timeZone)
1731</pre>
1732<p>
1733prints
1734</p>
1735<pre>
1736map[string] int
1737</pre>
1738<p>
1739If you want to control the default format for a custom type, all that's required is to define
1740a method with the signature <code>String() string</code> on the type.
1741For our simple type <code>T</code>, that might look like this.
1742</p>
1743<pre>
1744func (t *T) String() string {
1745 return fmt.Sprintf("%d/%g/%q", t.a, t.b, t.c)
1746}
1747fmt.Printf("%v\n", t)
1748</pre>
1749<p>
1750to print in the format
1751</p>
1752<pre>
17537/-2.35/"abc\tdef"
1754</pre>
1755<p>
1756(If you need to print <em>values</em> of type <code>T</code> as well as pointers to <code>T</code>,
1757the receiver for <code>String</code> must be of value type; this example used a pointer because
1758that's more efficient and idiomatic for struct types.
1759See the section below on <a href="#pointers_vs_values">pointers vs. value receivers</a> for more information.)
1760</p>
1761
1762<p>
1763Our <code>String</code> method is able to call <code>Sprintf</code> because the
1764print routines are fully reentrant and can be wrapped this way.
1765There is one important detail to understand about this approach,
1766however: don't construct a <code>String</code> method by calling
1767<code>Sprintf</code> in a way that will recur into your <code>String</code>
1768method indefinitely. This can happen if the <code>Sprintf</code>
1769call attempts to print the receiver directly as a string, which in
1770turn will invoke the method again. It's a common and easy mistake
1771to make, as this example shows.
1772</p>
1773
1774<pre>
1775type MyString string
1776
1777func (m MyString) String() string {
1778 return fmt.Sprintf("MyString=%s", m) // Error: will recur forever.
1779}
1780</pre>
1781
1782<p>
1783It's also easy to fix: convert the argument to the basic string type, which does not have the
1784method.
1785</p>
1786
1787<pre>
1788type MyString string
1789func (m MyString) String() string {
1790 return fmt.Sprintf("MyString=%s", string(m)) // OK: note conversion.
1791}
1792</pre>
1793
1794<p>
1795In the <a href="#initialization">initialization section</a> we'll see another technique that avoids this recursion.
1796</p>
1797
1798<p>
1799Another printing technique is to pass a print routine's arguments directly to another such routine.
1800The signature of <code>Printf</code> uses the type <code>...interface{}</code>
1801for its final argument to specify that an arbitrary number of parameters (of arbitrary type)
1802can appear after the format.
1803</p>
1804<pre>
1805func Printf(format string, v ...interface{}) (n int, err error) {
1806</pre>
1807<p>
1808Within the function <code>Printf</code>, <code>v</code> acts like a variable of type
1809<code>[]interface{}</code> but if it is passed to another variadic function, it acts like
1810a regular list of arguments.
1811Here is the implementation of the
1812function <code>log.Println</code> we used above. It passes its arguments directly to
1813<code>fmt.Sprintln</code> for the actual formatting.
1814</p>
1815<pre>
1816// Println prints to the standard logger in the manner of fmt.Println.
1817func Println(v ...interface{}) {
1818 std.Output(2, fmt.Sprintln(v...)) // Output takes parameters (int, string)
1819}
1820</pre>
1821<p>
1822We write <code>...</code> after <code>v</code> in the nested call to <code>Sprintln</code> to tell the
1823compiler to treat <code>v</code> as a list of arguments; otherwise it would just pass
1824<code>v</code> as a single slice argument.
1825</p>
1826<p>
1827There's even more to printing than we've covered here. See the <code>godoc</code> documentation
1828for package <code>fmt</code> for the details.
1829</p>
1830<p>
1831By the way, a <code>...</code> parameter can be of a specific type, for instance <code>...int</code>
1832for a min function that chooses the least of a list of integers:
1833</p>
1834<pre>
1835func Min(a ...int) int {
1836 min := int(^uint(0) >> 1) // largest int
1837 for _, i := range a {
1838 if i &lt; min {
1839 min = i
1840 }
1841 }
1842 return min
1843}
1844</pre>
1845
1846<h3 id="append">Append</h3>
1847<p>
1848Now we have the missing piece we needed to explain the design of
1849the <code>append</code> built-in function. The signature of <code>append</code>
1850is different from our custom <code>Append</code> function above.
1851Schematically, it's like this:
1852</p>
1853<pre>
1854func append(slice []<i>T</i>, elements ...<i>T</i>) []<i>T</i>
1855</pre>
1856<p>
1857where <i>T</i> is a placeholder for any given type. You can't
1858actually write a function in Go where the type <code>T</code>
1859is determined by the caller.
1860That's why <code>append</code> is built in: it needs support from the
1861compiler.
1862</p>
1863<p>
1864What <code>append</code> does is append the elements to the end of
1865the slice and return the result. The result needs to be returned
1866because, as with our hand-written <code>Append</code>, the underlying
1867array may change. This simple example
1868</p>
1869<pre>
1870x := []int{1,2,3}
1871x = append(x, 4, 5, 6)
1872fmt.Println(x)
1873</pre>
1874<p>
1875prints <code>[1 2 3 4 5 6]</code>. So <code>append</code> works a
1876little like <code>Printf</code>, collecting an arbitrary number of
1877arguments.
1878</p>
1879<p>
1880But what if we wanted to do what our <code>Append</code> does and
1881append a slice to a slice? Easy: use <code>...</code> at the call
1882site, just as we did in the call to <code>Output</code> above. This
1883snippet produces identical output to the one above.
1884</p>
1885<pre>
1886x := []int{1,2,3}
1887y := []int{4,5,6}
1888x = append(x, y...)
1889fmt.Println(x)
1890</pre>
1891<p>
1892Without that <code>...</code>, it wouldn't compile because the types
1893would be wrong; <code>y</code> is not of type <code>int</code>.
1894</p>
1895
1896<h2 id="initialization">Initialization</h2>
1897
1898<p>
1899Although it doesn't look superficially very different from
1900initialization in C or C++, initialization in Go is more powerful.
1901Complex structures can be built during initialization and the ordering
1902issues among initialized objects, even among different packages, are handled
1903correctly.
1904</p>
1905
1906<h3 id="constants">Constants</h3>
1907
1908<p>
1909Constants in Go are just that&mdash;constant.
1910They are created at compile time, even when defined as
1911locals in functions,
1912and can only be numbers, characters (runes), strings or booleans.
1913Because of the compile-time restriction, the expressions
1914that define them must be constant expressions,
1915evaluatable by the compiler. For instance,
1916<code>1&lt;&lt;3</code> is a constant expression, while
1917<code>math.Sin(math.Pi/4)</code> is not because
1918the function call to <code>math.Sin</code> needs
1919to happen at run time.
1920</p>
1921
1922<p>
1923In Go, enumerated constants are created using the <code>iota</code>
1924enumerator. Since <code>iota</code> can be part of an expression and
1925expressions can be implicitly repeated, it is easy to build intricate
1926sets of values.
1927</p>
1928{{code "/doc/progs/eff_bytesize.go" `/^type ByteSize/` `/^\)/`}}
1929<p>
1930The ability to attach a method such as <code>String</code> to any
1931user-defined type makes it possible for arbitrary values to format themselves
1932automatically for printing.
1933Although you'll see it most often applied to structs, this technique is also useful for
1934scalar types such as floating-point types like <code>ByteSize</code>.
1935</p>
1936{{code "/doc/progs/eff_bytesize.go" `/^func.*ByteSize.*String/` `/^}/`}}
1937<p>
1938The expression <code>YB</code> prints as <code>1.00YB</code>,
1939while <code>ByteSize(1e13)</code> prints as <code>9.09TB</code>.
1940</p>
1941
1942<p>
1943The use here of <code>Sprintf</code>
1944to implement <code>ByteSize</code>'s <code>String</code> method is safe
1945(avoids recurring indefinitely) not because of a conversion but
1946because it calls <code>Sprintf</code> with <code>%f</code>,
1947which is not a string format: <code>Sprintf</code> will only call
1948the <code>String</code> method when it wants a string, and <code>%f</code>
1949wants a floating-point value.
1950</p>
1951
1952<h3 id="variables">Variables</h3>
1953
1954<p>
1955Variables can be initialized just like constants but the
1956initializer can be a general expression computed at run time.
1957</p>
1958<pre>
1959var (
1960 home = os.Getenv("HOME")
1961 user = os.Getenv("USER")
1962 gopath = os.Getenv("GOPATH")
1963)
1964</pre>
1965
1966<h3 id="init">The init function</h3>
1967
1968<p>
1969Finally, each source file can define its own niladic <code>init</code> function to
1970set up whatever state is required. (Actually each file can have multiple
1971<code>init</code> functions.)
1972And finally means finally: <code>init</code> is called after all the
1973variable declarations in the package have evaluated their initializers,
1974and those are evaluated only after all the imported packages have been
1975initialized.
1976</p>
1977<p>
1978Besides initializations that cannot be expressed as declarations,
1979a common use of <code>init</code> functions is to verify or repair
1980correctness of the program state before real execution begins.
1981</p>
1982
1983<pre>
1984func init() {
1985 if user == "" {
1986 log.Fatal("$USER not set")
1987 }
1988 if home == "" {
1989 home = "/home/" + user
1990 }
1991 if gopath == "" {
1992 gopath = home + "/go"
1993 }
1994 // gopath may be overridden by --gopath flag on command line.
1995 flag.StringVar(&amp;gopath, "gopath", gopath, "override default GOPATH")
1996}
1997</pre>
1998
1999<h2 id="methods">Methods</h2>
2000
2001<h3 id="pointers_vs_values">Pointers vs. Values</h3>
2002<p>
2003As we saw with <code>ByteSize</code>,
2004methods can be defined for any named type (except a pointer or an interface);
2005the receiver does not have to be a struct.
2006</p>
2007<p>
2008In the discussion of slices above, we wrote an <code>Append</code>
2009function. We can define it as a method on slices instead. To do
2010this, we first declare a named type to which we can bind the method, and
2011then make the receiver for the method a value of that type.
2012</p>
2013<pre>
2014type ByteSlice []byte
2015
2016func (slice ByteSlice) Append(data []byte) []byte {
Dan Willemsen38f2dba2016-07-08 14:54:35 -07002017 // Body exactly the same as the Append function defined above.
Brent Austinba3052e2015-04-21 16:08:23 -07002018}
2019</pre>
2020<p>
2021This still requires the method to return the updated slice. We can
2022eliminate that clumsiness by redefining the method to take a
2023<i>pointer</i> to a <code>ByteSlice</code> as its receiver, so the
2024method can overwrite the caller's slice.
2025</p>
2026<pre>
2027func (p *ByteSlice) Append(data []byte) {
2028 slice := *p
2029 // Body as above, without the return.
2030 *p = slice
2031}
2032</pre>
2033<p>
2034In fact, we can do even better. If we modify our function so it looks
2035like a standard <code>Write</code> method, like this,
2036</p>
2037<pre>
2038func (p *ByteSlice) Write(data []byte) (n int, err error) {
2039 slice := *p
2040 // Again as above.
2041 *p = slice
2042 return len(data), nil
2043}
2044</pre>
2045<p>
2046then the type <code>*ByteSlice</code> satisfies the standard interface
2047<code>io.Writer</code>, which is handy. For instance, we can
2048print into one.
2049</p>
2050<pre>
2051 var b ByteSlice
2052 fmt.Fprintf(&amp;b, "This hour has %d days\n", 7)
2053</pre>
2054<p>
2055We pass the address of a <code>ByteSlice</code>
2056because only <code>*ByteSlice</code> satisfies <code>io.Writer</code>.
2057The rule about pointers vs. values for receivers is that value methods
2058can be invoked on pointers and values, but pointer methods can only be
2059invoked on pointers.
2060</p>
2061
2062<p>
2063This rule arises because pointer methods can modify the receiver; invoking
2064them on a value would cause the method to receive a copy of the value, so
2065any modifications would be discarded.
2066The language therefore disallows this mistake.
2067There is a handy exception, though. When the value is addressable, the
2068language takes care of the common case of invoking a pointer method on a
2069value by inserting the address operator automatically.
2070In our example, the variable <code>b</code> is addressable, so we can call
2071its <code>Write</code> method with just <code>b.Write</code>. The compiler
2072will rewrite that to <code>(&amp;b).Write</code> for us.
2073</p>
2074
2075<p>
2076By the way, the idea of using <code>Write</code> on a slice of bytes
2077is central to the implementation of <code>bytes.Buffer</code>.
2078</p>
2079
2080<h2 id="interfaces_and_types">Interfaces and other types</h2>
2081
2082<h3 id="interfaces">Interfaces</h3>
2083<p>
2084Interfaces in Go provide a way to specify the behavior of an
2085object: if something can do <em>this</em>, then it can be used
2086<em>here</em>. We've seen a couple of simple examples already;
2087custom printers can be implemented by a <code>String</code> method
2088while <code>Fprintf</code> can generate output to anything
2089with a <code>Write</code> method.
2090Interfaces with only one or two methods are common in Go code, and are
2091usually given a name derived from the method, such as <code>io.Writer</code>
2092for something that implements <code>Write</code>.
2093</p>
2094<p>
2095A type can implement multiple interfaces.
2096For instance, a collection can be sorted
2097by the routines in package <code>sort</code> if it implements
2098<code>sort.Interface</code>, which contains <code>Len()</code>,
2099<code>Less(i, j int) bool</code>, and <code>Swap(i, j int)</code>,
2100and it could also have a custom formatter.
2101In this contrived example <code>Sequence</code> satisfies both.
2102</p>
2103{{code "/doc/progs/eff_sequence.go" `/^type/` "$"}}
2104
2105<h3 id="conversions">Conversions</h3>
2106
2107<p>
2108The <code>String</code> method of <code>Sequence</code> is recreating the
2109work that <code>Sprint</code> already does for slices. We can share the
2110effort if we convert the <code>Sequence</code> to a plain
2111<code>[]int</code> before calling <code>Sprint</code>.
2112</p>
2113<pre>
2114func (s Sequence) String() string {
2115 sort.Sort(s)
2116 return fmt.Sprint([]int(s))
2117}
2118</pre>
2119<p>
2120This method is another example of the conversion technique for calling
2121<code>Sprintf</code> safely from a <code>String</code> method.
2122Because the two types (<code>Sequence</code> and <code>[]int</code>)
2123are the same if we ignore the type name, it's legal to convert between them.
2124The conversion doesn't create a new value, it just temporarily acts
2125as though the existing value has a new type.
2126(There are other legal conversions, such as from integer to floating point, that
2127do create a new value.)
2128</p>
2129<p>
2130It's an idiom in Go programs to convert the
2131type of an expression to access a different
2132set of methods. As an example, we could use the existing
2133type <code>sort.IntSlice</code> to reduce the entire example
2134to this:
2135</p>
2136<pre>
2137type Sequence []int
2138
2139// Method for printing - sorts the elements before printing
2140func (s Sequence) String() string {
2141 sort.IntSlice(s).Sort()
2142 return fmt.Sprint([]int(s))
2143}
2144</pre>
2145<p>
2146Now, instead of having <code>Sequence</code> implement multiple
2147interfaces (sorting and printing), we're using the ability of a data item to be
2148converted to multiple types (<code>Sequence</code>, <code>sort.IntSlice</code>
2149and <code>[]int</code>), each of which does some part of the job.
2150That's more unusual in practice but can be effective.
2151</p>
2152
2153<h3 id="interface_conversions">Interface conversions and type assertions</h3>
2154
2155<p>
2156<a href="#type_switch">Type switches</a> are a form of conversion: they take an interface and, for
2157each case in the switch, in a sense convert it to the type of that case.
2158Here's a simplified version of how the code under <code>fmt.Printf</code> turns a value into
2159a string using a type switch.
2160If it's already a string, we want the actual string value held by the interface, while if it has a
2161<code>String</code> method we want the result of calling the method.
2162</p>
2163
2164<pre>
2165type Stringer interface {
2166 String() string
2167}
2168
2169var value interface{} // Value provided by caller.
2170switch str := value.(type) {
2171case string:
2172 return str
2173case Stringer:
2174 return str.String()
2175}
2176</pre>
2177
2178<p>
2179The first case finds a concrete value; the second converts the interface into another interface.
2180It's perfectly fine to mix types this way.
2181</p>
2182
2183<p>
2184What if there's only one type we care about? If we know the value holds a <code>string</code>
2185and we just want to extract it?
2186A one-case type switch would do, but so would a <em>type assertion</em>.
2187A type assertion takes an interface value and extracts from it a value of the specified explicit type.
2188The syntax borrows from the clause opening a type switch, but with an explicit
2189type rather than the <code>type</code> keyword:
2190</p>
2191
2192<pre>
2193value.(typeName)
2194</pre>
2195
2196<p>
2197and the result is a new value with the static type <code>typeName</code>.
2198That type must either be the concrete type held by the interface, or a second interface
2199type that the value can be converted to.
2200To extract the string we know is in the value, we could write:
2201</p>
2202
2203<pre>
2204str := value.(string)
2205</pre>
2206
2207<p>
2208But if it turns out that the value does not contain a string, the program will crash with a run-time error.
2209To guard against that, use the "comma, ok" idiom to test, safely, whether the value is a string:
2210</p>
2211
2212<pre>
2213str, ok := value.(string)
2214if ok {
2215 fmt.Printf("string value is: %q\n", str)
2216} else {
2217 fmt.Printf("value is not a string\n")
2218}
2219</pre>
2220
2221<p>
2222If the type assertion fails, <code>str</code> will still exist and be of type string, but it will have
2223the zero value, an empty string.
2224</p>
2225
2226<p>
2227As an illustration of the capability, here's an <code>if</code>-<code>else</code>
2228statement that's equivalent to the type switch that opened this section.
2229</p>
2230
2231<pre>
2232if str, ok := value.(string); ok {
2233 return str
2234} else if str, ok := value.(Stringer); ok {
2235 return str.String()
2236}
2237</pre>
2238
2239<h3 id="generality">Generality</h3>
2240<p>
Dan Willemsend4b81d32016-08-16 14:40:48 -07002241If a type exists only to implement an interface and will
2242never have exported methods beyond that interface, there is
2243no need to export the type itself.
2244Exporting just the interface makes it clear the value has no
2245interesting behavior beyond what is described in the
2246interface.
Brent Austinba3052e2015-04-21 16:08:23 -07002247It also avoids the need to repeat the documentation
2248on every instance of a common method.
2249</p>
2250<p>
2251In such cases, the constructor should return an interface value
2252rather than the implementing type.
2253As an example, in the hash libraries
2254both <code>crc32.NewIEEE</code> and <code>adler32.New</code>
2255return the interface type <code>hash.Hash32</code>.
2256Substituting the CRC-32 algorithm for Adler-32 in a Go program
2257requires only changing the constructor call;
2258the rest of the code is unaffected by the change of algorithm.
2259</p>
2260<p>
2261A similar approach allows the streaming cipher algorithms
2262in the various <code>crypto</code> packages to be
2263separated from the block ciphers they chain together.
2264The <code>Block</code> interface
2265in the <code>crypto/cipher</code> package specifies the
2266behavior of a block cipher, which provides encryption
2267of a single block of data.
2268Then, by analogy with the <code>bufio</code> package,
2269cipher packages that implement this interface
2270can be used to construct streaming ciphers, represented
2271by the <code>Stream</code> interface, without
2272knowing the details of the block encryption.
2273</p>
2274<p>
2275The <code>crypto/cipher</code> interfaces look like this:
2276</p>
2277<pre>
2278type Block interface {
2279 BlockSize() int
2280 Encrypt(src, dst []byte)
2281 Decrypt(src, dst []byte)
2282}
2283
2284type Stream interface {
2285 XORKeyStream(dst, src []byte)
2286}
2287</pre>
2288
2289<p>
2290Here's the definition of the counter mode (CTR) stream,
2291which turns a block cipher into a streaming cipher; notice
2292that the block cipher's details are abstracted away:
2293</p>
2294
2295<pre>
2296// NewCTR returns a Stream that encrypts/decrypts using the given Block in
2297// counter mode. The length of iv must be the same as the Block's block size.
2298func NewCTR(block Block, iv []byte) Stream
2299</pre>
2300<p>
2301<code>NewCTR</code> applies not
2302just to one specific encryption algorithm and data source but to any
2303implementation of the <code>Block</code> interface and any
2304<code>Stream</code>. Because they return
2305interface values, replacing CTR
2306encryption with other encryption modes is a localized change. The constructor
2307calls must be edited, but because the surrounding code must treat the result only
2308as a <code>Stream</code>, it won't notice the difference.
2309</p>
2310
2311<h3 id="interface_methods">Interfaces and methods</h3>
2312<p>
2313Since almost anything can have methods attached, almost anything can
2314satisfy an interface. One illustrative example is in the <code>http</code>
2315package, which defines the <code>Handler</code> interface. Any object
2316that implements <code>Handler</code> can serve HTTP requests.
2317</p>
2318<pre>
2319type Handler interface {
2320 ServeHTTP(ResponseWriter, *Request)
2321}
2322</pre>
2323<p>
2324<code>ResponseWriter</code> is itself an interface that provides access
2325to the methods needed to return the response to the client.
2326Those methods include the standard <code>Write</code> method, so an
2327<code>http.ResponseWriter</code> can be used wherever an <code>io.Writer</code>
2328can be used.
2329<code>Request</code> is a struct containing a parsed representation
2330of the request from the client.
2331</p>
2332<p>
2333For brevity, let's ignore POSTs and assume HTTP requests are always
2334GETs; that simplification does not affect the way the handlers are
2335set up. Here's a trivial but complete implementation of a handler to
2336count the number of times the
2337page is visited.
2338</p>
2339<pre>
2340// Simple counter server.
2341type Counter struct {
2342 n int
2343}
2344
2345func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
2346 ctr.n++
2347 fmt.Fprintf(w, "counter = %d\n", ctr.n)
2348}
2349</pre>
2350<p>
2351(Keeping with our theme, note how <code>Fprintf</code> can print to an
2352<code>http.ResponseWriter</code>.)
2353For reference, here's how to attach such a server to a node on the URL tree.
2354</p>
2355<pre>
2356import "net/http"
2357...
2358ctr := new(Counter)
2359http.Handle("/counter", ctr)
2360</pre>
2361<p>
2362But why make <code>Counter</code> a struct? An integer is all that's needed.
2363(The receiver needs to be a pointer so the increment is visible to the caller.)
2364</p>
2365<pre>
2366// Simpler counter server.
2367type Counter int
2368
2369func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
2370 *ctr++
2371 fmt.Fprintf(w, "counter = %d\n", *ctr)
2372}
2373</pre>
2374<p>
2375What if your program has some internal state that needs to be notified that a page
2376has been visited? Tie a channel to the web page.
2377</p>
2378<pre>
2379// A channel that sends a notification on each visit.
2380// (Probably want the channel to be buffered.)
2381type Chan chan *http.Request
2382
2383func (ch Chan) ServeHTTP(w http.ResponseWriter, req *http.Request) {
2384 ch &lt;- req
2385 fmt.Fprint(w, "notification sent")
2386}
2387</pre>
2388<p>
2389Finally, let's say we wanted to present on <code>/args</code> the arguments
2390used when invoking the server binary.
2391It's easy to write a function to print the arguments.
2392</p>
2393<pre>
2394func ArgServer() {
2395 fmt.Println(os.Args)
2396}
2397</pre>
2398<p>
2399How do we turn that into an HTTP server? We could make <code>ArgServer</code>
2400a method of some type whose value we ignore, but there's a cleaner way.
2401Since we can define a method for any type except pointers and interfaces,
2402we can write a method for a function.
2403The <code>http</code> package contains this code:
2404</p>
2405<pre>
2406// The HandlerFunc type is an adapter to allow the use of
2407// ordinary functions as HTTP handlers. If f is a function
2408// with the appropriate signature, HandlerFunc(f) is a
2409// Handler object that calls f.
2410type HandlerFunc func(ResponseWriter, *Request)
2411
2412// ServeHTTP calls f(c, req).
2413func (f HandlerFunc) ServeHTTP(w ResponseWriter, req *Request) {
2414 f(w, req)
2415}
2416</pre>
2417<p>
2418<code>HandlerFunc</code> is a type with a method, <code>ServeHTTP</code>,
2419so values of that type can serve HTTP requests. Look at the implementation
2420of the method: the receiver is a function, <code>f</code>, and the method
2421calls <code>f</code>. That may seem odd but it's not that different from, say,
2422the receiver being a channel and the method sending on the channel.
2423</p>
2424<p>
2425To make <code>ArgServer</code> into an HTTP server, we first modify it
2426to have the right signature.
2427</p>
2428<pre>
2429// Argument server.
2430func ArgServer(w http.ResponseWriter, req *http.Request) {
2431 fmt.Fprintln(w, os.Args)
2432}
2433</pre>
2434<p>
2435<code>ArgServer</code> now has same signature as <code>HandlerFunc</code>,
2436so it can be converted to that type to access its methods,
2437just as we converted <code>Sequence</code> to <code>IntSlice</code>
2438to access <code>IntSlice.Sort</code>.
2439The code to set it up is concise:
2440</p>
2441<pre>
2442http.Handle("/args", http.HandlerFunc(ArgServer))
2443</pre>
2444<p>
2445When someone visits the page <code>/args</code>,
2446the handler installed at that page has value <code>ArgServer</code>
2447and type <code>HandlerFunc</code>.
2448The HTTP server will invoke the method <code>ServeHTTP</code>
2449of that type, with <code>ArgServer</code> as the receiver, which will in turn call
2450<code>ArgServer</code> (via the invocation <code>f(c, req)</code>
2451inside <code>HandlerFunc.ServeHTTP</code>).
2452The arguments will then be displayed.
2453</p>
2454<p>
2455In this section we have made an HTTP server from a struct, an integer,
2456a channel, and a function, all because interfaces are just sets of
2457methods, which can be defined for (almost) any type.
2458</p>
2459
2460<h2 id="blank">The blank identifier</h2>
2461
2462<p>
2463We've mentioned the blank identifier a couple of times now, in the context of
2464<a href="#for"><code>for</code> <code>range</code> loops</a>
2465and <a href="#maps">maps</a>.
2466The blank identifier can be assigned or declared with any value of any type, with the
2467value discarded harmlessly.
2468It's a bit like writing to the Unix <code>/dev/null</code> file:
2469it represents a write-only value
2470to be used as a place-holder
2471where a variable is needed but the actual value is irrelevant.
2472It has uses beyond those we've seen already.
2473</p>
2474
2475<h3 id="blank_assign">The blank identifier in multiple assignment</h3>
2476
2477<p>
2478The use of a blank identifier in a <code>for</code> <code>range</code> loop is a
2479special case of a general situation: multiple assignment.
2480</p>
2481
2482<p>
2483If an assignment requires multiple values on the left side,
2484but one of the values will not be used by the program,
2485a blank identifier on the left-hand-side of
2486the assignment avoids the need
2487to create a dummy variable and makes it clear that the
2488value is to be discarded.
2489For instance, when calling a function that returns
2490a value and an error, but only the error is important,
2491use the blank identifier to discard the irrelevant value.
2492</p>
2493
2494<pre>
2495if _, err := os.Stat(path); os.IsNotExist(err) {
2496 fmt.Printf("%s does not exist\n", path)
2497}
2498</pre>
2499
2500<p>
2501Occasionally you'll see code that discards the error value in order
2502to ignore the error; this is terrible practice. Always check error returns;
2503they're provided for a reason.
2504</p>
2505
2506<pre>
2507// Bad! This code will crash if path does not exist.
2508fi, _ := os.Stat(path)
2509if fi.IsDir() {
2510 fmt.Printf("%s is a directory\n", path)
2511}
2512</pre>
2513
2514<h3 id="blank_unused">Unused imports and variables</h3>
2515
2516<p>
2517It is an error to import a package or to declare a variable without using it.
2518Unused imports bloat the program and slow compilation,
2519while a variable that is initialized but not used is at least
2520a wasted computation and perhaps indicative of a
2521larger bug.
2522When a program is under active development, however,
2523unused imports and variables often arise and it can
2524be annoying to delete them just to have the compilation proceed,
2525only to have them be needed again later.
2526The blank identifier provides a workaround.
2527</p>
2528<p>
2529This half-written program has two unused imports
2530(<code>fmt</code> and <code>io</code>)
2531and an unused variable (<code>fd</code>),
2532so it will not compile, but it would be nice to see if the
2533code so far is correct.
2534</p>
2535{{code "/doc/progs/eff_unused1.go" `/package/` `$`}}
2536<p>
2537To silence complaints about the unused imports, use a
2538blank identifier to refer to a symbol from the imported package.
2539Similarly, assigning the unused variable <code>fd</code>
2540to the blank identifier will silence the unused variable error.
2541This version of the program does compile.
2542</p>
2543{{code "/doc/progs/eff_unused2.go" `/package/` `$`}}
2544
2545<p>
2546By convention, the global declarations to silence import errors
2547should come right after the imports and be commented,
2548both to make them easy to find and as a reminder to clean things up later.
2549</p>
2550
2551<h3 id="blank_import">Import for side effect</h3>
2552
2553<p>
2554An unused import like <code>fmt</code> or <code>io</code> in the
2555previous example should eventually be used or removed:
2556blank assignments identify code as a work in progress.
2557But sometimes it is useful to import a package only for its
2558side effects, without any explicit use.
2559For example, during its <code>init</code> function,
2560the <code><a href="/pkg/net/http/pprof/">net/http/pprof</a></code>
2561package registers HTTP handlers that provide
2562debugging information. It has an exported API, but
2563most clients need only the handler registration and
2564access the data through a web page.
2565To import the package only for its side effects, rename the package
2566to the blank identifier:
2567</p>
2568<pre>
2569import _ "net/http/pprof"
2570</pre>
2571<p>
2572This form of import makes clear that the package is being
2573imported for its side effects, because there is no other possible
2574use of the package: in this file, it doesn't have a name.
2575(If it did, and we didn't use that name, the compiler would reject the program.)
2576</p>
2577
2578<h3 id="blank_implements">Interface checks</h3>
2579
2580<p>
2581As we saw in the discussion of <a href="#interfaces_and_types">interfaces</a> above,
2582a type need not declare explicitly that it implements an interface.
2583Instead, a type implements the interface just by implementing the interface's methods.
2584In practice, most interface conversions are static and therefore checked at compile time.
2585For example, passing an <code>*os.File</code> to a function
2586expecting an <code>io.Reader</code> will not compile unless
2587<code>*os.File</code> implements the <code>io.Reader</code> interface.
2588</p>
2589
2590<p>
2591Some interface checks do happen at run-time, though.
2592One instance is in the <code><a href="/pkg/encoding/json/">encoding/json</a></code>
2593package, which defines a <code><a href="/pkg/encoding/json/#Marshaler">Marshaler</a></code>
2594interface. When the JSON encoder receives a value that implements that interface,
2595the encoder invokes the value's marshaling method to convert it to JSON
2596instead of doing the standard conversion.
2597The encoder checks this property at run time with a <a href="#interface_conversions">type assertion</a> like:
2598</p>
2599
2600<pre>
2601m, ok := val.(json.Marshaler)
2602</pre>
2603
2604<p>
2605If it's necessary only to ask whether a type implements an interface, without
2606actually using the interface itself, perhaps as part of an error check, use the blank
2607identifier to ignore the type-asserted value:
2608</p>
2609
2610<pre>
2611if _, ok := val.(json.Marshaler); ok {
2612 fmt.Printf("value %v of type %T implements json.Marshaler\n", val, val)
2613}
2614</pre>
2615
2616<p>
2617One place this situation arises is when it is necessary to guarantee within the package implementing the type that
2618it actually satisfies the interface.
2619If a type—for example,
2620<code><a href="/pkg/encoding/json/#RawMessage">json.RawMessage</a></code>—needs
2621a custom JSON representation, it should implement
2622<code>json.Marshaler</code>, but there are no static conversions that would
2623cause the compiler to verify this automatically.
2624If the type inadvertently fails to satisfy the interface, the JSON encoder will still work,
2625but will not use the custom implementation.
2626To guarantee that the implementation is correct,
2627a global declaration using the blank identifier can be used in the package:
2628</p>
2629<pre>
2630var _ json.Marshaler = (*RawMessage)(nil)
2631</pre>
2632<p>
2633In this declaration, the assignment involving a conversion of a
2634<code>*RawMessage</code> to a <code>Marshaler</code>
2635requires that <code>*RawMessage</code> implements <code>Marshaler</code>,
2636and that property will be checked at compile time.
2637Should the <code>json.Marshaler</code> interface change, this package
2638will no longer compile and we will be on notice that it needs to be updated.
2639</p>
2640
2641<p>
2642The appearance of the blank identifier in this construct indicates that
2643the declaration exists only for the type checking,
2644not to create a variable.
2645Don't do this for every type that satisfies an interface, though.
2646By convention, such declarations are only used
2647when there are no static conversions already present in the code,
2648which is a rare event.
2649</p>
2650
2651
2652<h2 id="embedding">Embedding</h2>
2653
2654<p>
2655Go does not provide the typical, type-driven notion of subclassing,
2656but it does have the ability to &ldquo;borrow&rdquo; pieces of an
2657implementation by <em>embedding</em> types within a struct or
2658interface.
2659</p>
2660<p>
2661Interface embedding is very simple.
2662We've mentioned the <code>io.Reader</code> and <code>io.Writer</code> interfaces before;
2663here are their definitions.
2664</p>
2665<pre>
2666type Reader interface {
2667 Read(p []byte) (n int, err error)
2668}
2669
2670type Writer interface {
2671 Write(p []byte) (n int, err error)
2672}
2673</pre>
2674<p>
2675The <code>io</code> package also exports several other interfaces
2676that specify objects that can implement several such methods.
2677For instance, there is <code>io.ReadWriter</code>, an interface
2678containing both <code>Read</code> and <code>Write</code>.
2679We could specify <code>io.ReadWriter</code> by listing the
2680two methods explicitly, but it's easier and more evocative
2681to embed the two interfaces to form the new one, like this:
2682</p>
2683<pre>
2684// ReadWriter is the interface that combines the Reader and Writer interfaces.
2685type ReadWriter interface {
2686 Reader
2687 Writer
2688}
2689</pre>
2690<p>
2691This says just what it looks like: A <code>ReadWriter</code> can do
2692what a <code>Reader</code> does <em>and</em> what a <code>Writer</code>
2693does; it is a union of the embedded interfaces (which must be disjoint
2694sets of methods).
2695Only interfaces can be embedded within interfaces.
2696</p>
2697<p>
2698The same basic idea applies to structs, but with more far-reaching
2699implications. The <code>bufio</code> package has two struct types,
2700<code>bufio.Reader</code> and <code>bufio.Writer</code>, each of
2701which of course implements the analogous interfaces from package
2702<code>io</code>.
2703And <code>bufio</code> also implements a buffered reader/writer,
2704which it does by combining a reader and a writer into one struct
2705using embedding: it lists the types within the struct
2706but does not give them field names.
2707</p>
2708<pre>
2709// ReadWriter stores pointers to a Reader and a Writer.
2710// It implements io.ReadWriter.
2711type ReadWriter struct {
2712 *Reader // *bufio.Reader
2713 *Writer // *bufio.Writer
2714}
2715</pre>
2716<p>
2717The embedded elements are pointers to structs and of course
2718must be initialized to point to valid structs before they
2719can be used.
2720The <code>ReadWriter</code> struct could be written as
2721</p>
2722<pre>
2723type ReadWriter struct {
2724 reader *Reader
2725 writer *Writer
2726}
2727</pre>
2728<p>
2729but then to promote the methods of the fields and to
2730satisfy the <code>io</code> interfaces, we would also need
2731to provide forwarding methods, like this:
2732</p>
2733<pre>
2734func (rw *ReadWriter) Read(p []byte) (n int, err error) {
2735 return rw.reader.Read(p)
2736}
2737</pre>
2738<p>
2739By embedding the structs directly, we avoid this bookkeeping.
2740The methods of embedded types come along for free, which means that <code>bufio.ReadWriter</code>
2741not only has the methods of <code>bufio.Reader</code> and <code>bufio.Writer</code>,
2742it also satisfies all three interfaces:
2743<code>io.Reader</code>,
2744<code>io.Writer</code>, and
2745<code>io.ReadWriter</code>.
2746</p>
2747<p>
2748There's an important way in which embedding differs from subclassing. When we embed a type,
2749the methods of that type become methods of the outer type,
2750but when they are invoked the receiver of the method is the inner type, not the outer one.
2751In our example, when the <code>Read</code> method of a <code>bufio.ReadWriter</code> is
2752invoked, it has exactly the same effect as the forwarding method written out above;
2753the receiver is the <code>reader</code> field of the <code>ReadWriter</code>, not the
2754<code>ReadWriter</code> itself.
2755</p>
2756<p>
2757Embedding can also be a simple convenience.
2758This example shows an embedded field alongside a regular, named field.
2759</p>
2760<pre>
2761type Job struct {
2762 Command string
2763 *log.Logger
2764}
2765</pre>
2766<p>
2767The <code>Job</code> type now has the <code>Log</code>, <code>Logf</code>
2768and other
2769methods of <code>*log.Logger</code>. We could have given the <code>Logger</code>
2770a field name, of course, but it's not necessary to do so. And now, once
2771initialized, we can
2772log to the <code>Job</code>:
2773</p>
2774<pre>
2775job.Log("starting now...")
2776</pre>
2777<p>
2778The <code>Logger</code> is a regular field of the <code>Job</code> struct,
2779so we can initialize it in the usual way inside the constructor for <code>Job</code>, like this,
2780</p>
2781<pre>
2782func NewJob(command string, logger *log.Logger) *Job {
2783 return &amp;Job{command, logger}
2784}
2785</pre>
2786<p>
2787or with a composite literal,
2788</p>
2789<pre>
2790job := &amp;Job{command, log.New(os.Stderr, "Job: ", log.Ldate)}
2791</pre>
2792<p>
2793If we need to refer to an embedded field directly, the type name of the field,
2794ignoring the package qualifier, serves as a field name, as it did
2795in the <code>Read</code> method of our <code>ReaderWriter</code> struct.
2796Here, if we needed to access the
2797<code>*log.Logger</code> of a <code>Job</code> variable <code>job</code>,
2798we would write <code>job.Logger</code>,
2799which would be useful if we wanted to refine the methods of <code>Logger</code>.
2800</p>
2801<pre>
2802func (job *Job) Logf(format string, args ...interface{}) {
2803 job.Logger.Logf("%q: %s", job.Command, fmt.Sprintf(format, args...))
2804}
2805</pre>
2806<p>
2807Embedding types introduces the problem of name conflicts but the rules to resolve
2808them are simple.
2809First, a field or method <code>X</code> hides any other item <code>X</code> in a more deeply
2810nested part of the type.
2811If <code>log.Logger</code> contained a field or method called <code>Command</code>, the <code>Command</code> field
2812of <code>Job</code> would dominate it.
2813</p>
2814<p>
2815Second, if the same name appears at the same nesting level, it is usually an error;
2816it would be erroneous to embed <code>log.Logger</code> if the <code>Job</code> struct
2817contained another field or method called <code>Logger</code>.
2818However, if the duplicate name is never mentioned in the program outside the type definition, it is OK.
2819This qualification provides some protection against changes made to types embedded from outside; there
2820is no problem if a field is added that conflicts with another field in another subtype if neither field
2821is ever used.
2822</p>
2823
2824
2825<h2 id="concurrency">Concurrency</h2>
2826
2827<h3 id="sharing">Share by communicating</h3>
2828
2829<p>
2830Concurrent programming is a large topic and there is space only for some
2831Go-specific highlights here.
2832</p>
2833<p>
2834Concurrent programming in many environments is made difficult by the
2835subtleties required to implement correct access to shared variables. Go encourages
2836a different approach in which shared values are passed around on channels
2837and, in fact, never actively shared by separate threads of execution.
2838Only one goroutine has access to the value at any given time.
2839Data races cannot occur, by design.
2840To encourage this way of thinking we have reduced it to a slogan:
2841</p>
2842<blockquote>
2843Do not communicate by sharing memory;
2844instead, share memory by communicating.
2845</blockquote>
2846<p>
2847This approach can be taken too far. Reference counts may be best done
2848by putting a mutex around an integer variable, for instance. But as a
2849high-level approach, using channels to control access makes it easier
2850to write clear, correct programs.
2851</p>
2852<p>
2853One way to think about this model is to consider a typical single-threaded
2854program running on one CPU. It has no need for synchronization primitives.
2855Now run another such instance; it too needs no synchronization. Now let those
2856two communicate; if the communication is the synchronizer, there's still no need
2857for other synchronization. Unix pipelines, for example, fit this model
2858perfectly. Although Go's approach to concurrency originates in Hoare's
2859Communicating Sequential Processes (CSP),
2860it can also be seen as a type-safe generalization of Unix pipes.
2861</p>
2862
2863<h3 id="goroutines">Goroutines</h3>
2864
2865<p>
2866They're called <em>goroutines</em> because the existing
2867terms&mdash;threads, coroutines, processes, and so on&mdash;convey
2868inaccurate connotations. A goroutine has a simple model: it is a
2869function executing concurrently with other goroutines in the same
2870address space. It is lightweight, costing little more than the
2871allocation of stack space.
2872And the stacks start small, so they are cheap, and grow
2873by allocating (and freeing) heap storage as required.
2874</p>
2875<p>
2876Goroutines are multiplexed onto multiple OS threads so if one should
2877block, such as while waiting for I/O, others continue to run. Their
2878design hides many of the complexities of thread creation and
2879management.
2880</p>
2881<p>
2882Prefix a function or method call with the <code>go</code>
2883keyword to run the call in a new goroutine.
2884When the call completes, the goroutine
2885exits, silently. (The effect is similar to the Unix shell's
2886<code>&amp;</code> notation for running a command in the
2887background.)
2888</p>
2889<pre>
2890go list.Sort() // run list.Sort concurrently; don't wait for it.
2891</pre>
2892<p>
2893A function literal can be handy in a goroutine invocation.
2894</p>
2895<pre>
2896func Announce(message string, delay time.Duration) {
2897 go func() {
2898 time.Sleep(delay)
2899 fmt.Println(message)
2900 }() // Note the parentheses - must call the function.
2901}
2902</pre>
2903<p>
2904In Go, function literals are closures: the implementation makes
2905sure the variables referred to by the function survive as long as they are active.
2906</p>
2907<p>
2908These examples aren't too practical because the functions have no way of signaling
2909completion. For that, we need channels.
2910</p>
2911
2912<h3 id="channels">Channels</h3>
2913
2914<p>
2915Like maps, channels are allocated with <code>make</code>, and
2916the resulting value acts as a reference to an underlying data structure.
2917If an optional integer parameter is provided, it sets the buffer size for the channel.
2918The default is zero, for an unbuffered or synchronous channel.
2919</p>
2920<pre>
2921ci := make(chan int) // unbuffered channel of integers
2922cj := make(chan int, 0) // unbuffered channel of integers
2923cs := make(chan *os.File, 100) // buffered channel of pointers to Files
2924</pre>
2925<p>
2926Unbuffered channels combine communication&mdash;the exchange of a value&mdash;with
2927synchronization&mdash;guaranteeing that two calculations (goroutines) are in
2928a known state.
2929</p>
2930<p>
2931There are lots of nice idioms using channels. Here's one to get us started.
2932In the previous section we launched a sort in the background. A channel
2933can allow the launching goroutine to wait for the sort to complete.
2934</p>
2935<pre>
2936c := make(chan int) // Allocate a channel.
2937// Start the sort in a goroutine; when it completes, signal on the channel.
2938go func() {
2939 list.Sort()
2940 c &lt;- 1 // Send a signal; value does not matter.
2941}()
2942doSomethingForAWhile()
2943&lt;-c // Wait for sort to finish; discard sent value.
2944</pre>
2945<p>
2946Receivers always block until there is data to receive.
2947If the channel is unbuffered, the sender blocks until the receiver has
2948received the value.
2949If the channel has a buffer, the sender blocks only until the
2950value has been copied to the buffer; if the buffer is full, this
2951means waiting until some receiver has retrieved a value.
2952</p>
2953<p>
2954A buffered channel can be used like a semaphore, for instance to
2955limit throughput. In this example, incoming requests are passed
2956to <code>handle</code>, which sends a value into the channel, processes
2957the request, and then receives a value from the channel
2958to ready the &ldquo;semaphore&rdquo; for the next consumer.
2959The capacity of the channel buffer limits the number of
2960simultaneous calls to <code>process</code>.
2961</p>
2962<pre>
2963var sem = make(chan int, MaxOutstanding)
2964
2965func handle(r *Request) {
2966 sem &lt;- 1 // Wait for active queue to drain.
2967 process(r) // May take a long time.
2968 &lt;-sem // Done; enable next request to run.
2969}
2970
2971func Serve(queue chan *Request) {
2972 for {
2973 req := &lt;-queue
2974 go handle(req) // Don't wait for handle to finish.
2975 }
2976}
2977</pre>
2978
2979<p>
2980Once <code>MaxOutstanding</code> handlers are executing <code>process</code>,
2981any more will block trying to send into the filled channel buffer,
2982until one of the existing handlers finishes and receives from the buffer.
2983</p>
2984
2985<p>
2986This design has a problem, though: <code>Serve</code>
2987creates a new goroutine for
2988every incoming request, even though only <code>MaxOutstanding</code>
2989of them can run at any moment.
2990As a result, the program can consume unlimited resources if the requests come in too fast.
2991We can address that deficiency by changing <code>Serve</code> to
2992gate the creation of the goroutines.
2993Here's an obvious solution, but beware it has a bug we'll fix subsequently:
2994</p>
2995
2996<pre>
2997func Serve(queue chan *Request) {
2998 for req := range queue {
2999 sem &lt;- 1
3000 go func() {
3001 process(req) // Buggy; see explanation below.
3002 &lt;-sem
3003 }()
3004 }
3005}</pre>
3006
3007<p>
3008The bug is that in a Go <code>for</code> loop, the loop variable
3009is reused for each iteration, so the <code>req</code>
3010variable is shared across all goroutines.
3011That's not what we want.
3012We need to make sure that <code>req</code> is unique for each goroutine.
3013Here's one way to do that, passing the value of <code>req</code> as an argument
3014to the closure in the goroutine:
3015</p>
3016
3017<pre>
3018func Serve(queue chan *Request) {
3019 for req := range queue {
3020 sem &lt;- 1
3021 go func(req *Request) {
3022 process(req)
3023 &lt;-sem
3024 }(req)
3025 }
3026}</pre>
3027
3028<p>
3029Compare this version with the previous to see the difference in how
3030the closure is declared and run.
3031Another solution is just to create a new variable with the same
3032name, as in this example:
3033</p>
3034
3035<pre>
3036func Serve(queue chan *Request) {
3037 for req := range queue {
3038 req := req // Create new instance of req for the goroutine.
3039 sem &lt;- 1
3040 go func() {
3041 process(req)
3042 &lt;-sem
3043 }()
3044 }
3045}</pre>
3046
3047<p>
3048It may seem odd to write
3049</p>
3050
3051<pre>
3052req := req
3053</pre>
3054
3055<p>
Dan Willemsen09eb3b12015-09-16 14:34:17 -07003056but it's legal and idiomatic in Go to do this.
Brent Austinba3052e2015-04-21 16:08:23 -07003057You get a fresh version of the variable with the same name, deliberately
3058shadowing the loop variable locally but unique to each goroutine.
3059</p>
3060
3061<p>
3062Going back to the general problem of writing the server,
3063another approach that manages resources well is to start a fixed
3064number of <code>handle</code> goroutines all reading from the request
3065channel.
3066The number of goroutines limits the number of simultaneous
3067calls to <code>process</code>.
3068This <code>Serve</code> function also accepts a channel on which
3069it will be told to exit; after launching the goroutines it blocks
3070receiving from that channel.
3071</p>
3072
3073<pre>
3074func handle(queue chan *Request) {
3075 for r := range queue {
3076 process(r)
3077 }
3078}
3079
3080func Serve(clientRequests chan *Request, quit chan bool) {
3081 // Start handlers
3082 for i := 0; i &lt; MaxOutstanding; i++ {
3083 go handle(clientRequests)
3084 }
3085 &lt;-quit // Wait to be told to exit.
3086}
3087</pre>
3088
3089<h3 id="chan_of_chan">Channels of channels</h3>
3090<p>
3091One of the most important properties of Go is that
3092a channel is a first-class value that can be allocated and passed
3093around like any other. A common use of this property is
3094to implement safe, parallel demultiplexing.
3095</p>
3096<p>
3097In the example in the previous section, <code>handle</code> was
3098an idealized handler for a request but we didn't define the
3099type it was handling. If that type includes a channel on which
3100to reply, each client can provide its own path for the answer.
3101Here's a schematic definition of type <code>Request</code>.
3102</p>
3103<pre>
3104type Request struct {
3105 args []int
3106 f func([]int) int
3107 resultChan chan int
3108}
3109</pre>
3110<p>
3111The client provides a function and its arguments, as well as
3112a channel inside the request object on which to receive the answer.
3113</p>
3114<pre>
3115func sum(a []int) (s int) {
3116 for _, v := range a {
3117 s += v
3118 }
3119 return
3120}
3121
3122request := &amp;Request{[]int{3, 4, 5}, sum, make(chan int)}
3123// Send request
3124clientRequests &lt;- request
3125// Wait for response.
3126fmt.Printf("answer: %d\n", &lt;-request.resultChan)
3127</pre>
3128<p>
3129On the server side, the handler function is the only thing that changes.
3130</p>
3131<pre>
3132func handle(queue chan *Request) {
3133 for req := range queue {
3134 req.resultChan &lt;- req.f(req.args)
3135 }
3136}
3137</pre>
3138<p>
3139There's clearly a lot more to do to make it realistic, but this
3140code is a framework for a rate-limited, parallel, non-blocking RPC
3141system, and there's not a mutex in sight.
3142</p>
3143
3144<h3 id="parallel">Parallelization</h3>
3145<p>
3146Another application of these ideas is to parallelize a calculation
3147across multiple CPU cores. If the calculation can be broken into
3148separate pieces that can execute independently, it can be parallelized,
3149with a channel to signal when each piece completes.
3150</p>
3151<p>
3152Let's say we have an expensive operation to perform on a vector of items,
3153and that the value of the operation on each item is independent,
3154as in this idealized example.
3155</p>
3156<pre>
3157type Vector []float64
3158
3159// Apply the operation to v[i], v[i+1] ... up to v[n-1].
3160func (v Vector) DoSome(i, n int, u Vector, c chan int) {
3161 for ; i &lt; n; i++ {
3162 v[i] += u.Op(v[i])
3163 }
3164 c &lt;- 1 // signal that this piece is done
3165}
3166</pre>
3167<p>
3168We launch the pieces independently in a loop, one per CPU.
3169They can complete in any order but it doesn't matter; we just
3170count the completion signals by draining the channel after
3171launching all the goroutines.
3172</p>
3173<pre>
Dan Willemsen09eb3b12015-09-16 14:34:17 -07003174const numCPU = 4 // number of CPU cores
Brent Austinba3052e2015-04-21 16:08:23 -07003175
3176func (v Vector) DoAll(u Vector) {
Dan Willemsen09eb3b12015-09-16 14:34:17 -07003177 c := make(chan int, numCPU) // Buffering optional but sensible.
3178 for i := 0; i &lt; numCPU; i++ {
3179 go v.DoSome(i*len(v)/numCPU, (i+1)*len(v)/numCPU, u, c)
Brent Austinba3052e2015-04-21 16:08:23 -07003180 }
3181 // Drain the channel.
Dan Willemsen09eb3b12015-09-16 14:34:17 -07003182 for i := 0; i &lt; numCPU; i++ {
Brent Austinba3052e2015-04-21 16:08:23 -07003183 &lt;-c // wait for one task to complete
3184 }
3185 // All done.
3186}
Brent Austinba3052e2015-04-21 16:08:23 -07003187</pre>
Brent Austinba3052e2015-04-21 16:08:23 -07003188<p>
Dan Willemsen09eb3b12015-09-16 14:34:17 -07003189Rather than create a constant value for numCPU, we can ask the runtime what
3190value is appropriate.
3191The function <code><a href="/pkg/runtime#NumCPU">runtime.NumCPU</a></code>
3192returns the number of hardware CPU cores in the machine, so we could write
Brent Austinba3052e2015-04-21 16:08:23 -07003193</p>
Dan Willemsen09eb3b12015-09-16 14:34:17 -07003194<pre>
3195var numCPU = runtime.NumCPU()
3196</pre>
3197<p>
3198There is also a function
3199<code><a href="/pkg/runtime#GOMAXPROCS">runtime.GOMAXPROCS</a></code>,
3200which reports (or sets)
3201the user-specified number of cores that a Go program can have running
3202simultaneously.
3203It defaults to the value of <code>runtime.NumCPU</code> but can be
3204overridden by setting the similarly named shell environment variable
3205or by calling the function with a positive number. Calling it with
3206zero just queries the value.
3207Therefore if we want to honor the user's resource request, we should write
3208</p>
3209<pre>
3210var numCPU = runtime.GOMAXPROCS(0)
3211</pre>
Brent Austinba3052e2015-04-21 16:08:23 -07003212<p>
3213Be sure not to confuse the ideas of concurrency—structuring a program
3214as independently executing components—and parallelism—executing
3215calculations in parallel for efficiency on multiple CPUs.
3216Although the concurrency features of Go can make some problems easy
3217to structure as parallel computations, Go is a concurrent language,
3218not a parallel one, and not all parallelization problems fit Go's model.
3219For a discussion of the distinction, see the talk cited in
3220<a href="//blog.golang.org/2013/01/concurrency-is-not-parallelism.html">this
3221blog post</a>.
3222
3223<h3 id="leaky_buffer">A leaky buffer</h3>
3224
3225<p>
3226The tools of concurrent programming can even make non-concurrent
3227ideas easier to express. Here's an example abstracted from an RPC
3228package. The client goroutine loops receiving data from some source,
3229perhaps a network. To avoid allocating and freeing buffers, it keeps
3230a free list, and uses a buffered channel to represent it. If the
3231channel is empty, a new buffer gets allocated.
3232Once the message buffer is ready, it's sent to the server on
3233<code>serverChan</code>.
3234</p>
3235<pre>
3236var freeList = make(chan *Buffer, 100)
3237var serverChan = make(chan *Buffer)
3238
3239func client() {
3240 for {
3241 var b *Buffer
3242 // Grab a buffer if available; allocate if not.
3243 select {
3244 case b = &lt;-freeList:
3245 // Got one; nothing more to do.
3246 default:
3247 // None free, so allocate a new one.
3248 b = new(Buffer)
3249 }
3250 load(b) // Read next message from the net.
3251 serverChan &lt;- b // Send to server.
3252 }
3253}
3254</pre>
3255<p>
3256The server loop receives each message from the client, processes it,
3257and returns the buffer to the free list.
3258</p>
3259<pre>
3260func server() {
3261 for {
3262 b := &lt;-serverChan // Wait for work.
3263 process(b)
3264 // Reuse buffer if there's room.
3265 select {
3266 case freeList &lt;- b:
3267 // Buffer on free list; nothing more to do.
3268 default:
3269 // Free list full, just carry on.
3270 }
3271 }
3272}
3273</pre>
3274<p>
3275The client attempts to retrieve a buffer from <code>freeList</code>;
3276if none is available, it allocates a fresh one.
3277The server's send to <code>freeList</code> puts <code>b</code> back
3278on the free list unless the list is full, in which case the
3279buffer is dropped on the floor to be reclaimed by
3280the garbage collector.
3281(The <code>default</code> clauses in the <code>select</code>
3282statements execute when no other case is ready,
3283meaning that the <code>selects</code> never block.)
3284This implementation builds a leaky bucket free list
3285in just a few lines, relying on the buffered channel and
3286the garbage collector for bookkeeping.
3287</p>
3288
3289<h2 id="errors">Errors</h2>
3290
3291<p>
3292Library routines must often return some sort of error indication to
3293the caller.
3294As mentioned earlier, Go's multivalue return makes it
3295easy to return a detailed error description alongside the normal
3296return value.
3297It is good style to use this feature to provide detailed error information.
3298For example, as we'll see, <code>os.Open</code> doesn't
3299just return a <code>nil</code> pointer on failure, it also returns an
3300error value that describes what went wrong.
3301</p>
3302
3303<p>
3304By convention, errors have type <code>error</code>,
3305a simple built-in interface.
3306</p>
3307<pre>
3308type error interface {
3309 Error() string
3310}
3311</pre>
3312<p>
3313A library writer is free to implement this interface with a
3314richer model under the covers, making it possible not only
3315to see the error but also to provide some context.
3316As mentioned, alongside the usual <code>*os.File</code>
3317return value, <code>os.Open</code> also returns an
3318error value.
3319If the file is opened successfully, the error will be <code>nil</code>,
3320but when there is a problem, it will hold an
3321<code>os.PathError</code>:
3322</p>
3323<pre>
3324// PathError records an error and the operation and
3325// file path that caused it.
3326type PathError struct {
3327 Op string // "open", "unlink", etc.
3328 Path string // The associated file.
3329 Err error // Returned by the system call.
3330}
3331
3332func (e *PathError) Error() string {
3333 return e.Op + " " + e.Path + ": " + e.Err.Error()
3334}
3335</pre>
3336<p>
3337<code>PathError</code>'s <code>Error</code> generates
3338a string like this:
3339</p>
3340<pre>
3341open /etc/passwx: no such file or directory
3342</pre>
3343<p>
3344Such an error, which includes the problematic file name, the
3345operation, and the operating system error it triggered, is useful even
3346if printed far from the call that caused it;
3347it is much more informative than the plain
3348"no such file or directory".
3349</p>
3350
3351<p>
3352When feasible, error strings should identify their origin, such as by having
3353a prefix naming the operation or package that generated the error. For example, in package
3354<code>image</code>, the string representation for a decoding error due to an
3355unknown format is "image: unknown format".
3356</p>
3357
3358<p>
3359Callers that care about the precise error details can
3360use a type switch or a type assertion to look for specific
3361errors and extract details. For <code>PathErrors</code>
3362this might include examining the internal <code>Err</code>
3363field for recoverable failures.
3364</p>
3365
3366<pre>
3367for try := 0; try &lt; 2; try++ {
3368 file, err = os.Create(filename)
3369 if err == nil {
3370 return
3371 }
3372 if e, ok := err.(*os.PathError); ok &amp;&amp; e.Err == syscall.ENOSPC {
3373 deleteTempFiles() // Recover some space.
3374 continue
3375 }
3376 return
3377}
3378</pre>
3379
3380<p>
3381The second <code>if</code> statement here is another <a href="#interface_conversions">type assertion</a>.
3382If it fails, <code>ok</code> will be false, and <code>e</code>
3383will be <code>nil</code>.
3384If it succeeds, <code>ok</code> will be true, which means the
3385error was of type <code>*os.PathError</code>, and then so is <code>e</code>,
3386which we can examine for more information about the error.
3387</p>
3388
3389<h3 id="panic">Panic</h3>
3390
3391<p>
3392The usual way to report an error to a caller is to return an
3393<code>error</code> as an extra return value. The canonical
3394<code>Read</code> method is a well-known instance; it returns a byte
3395count and an <code>error</code>. But what if the error is
3396unrecoverable? Sometimes the program simply cannot continue.
3397</p>
3398
3399<p>
3400For this purpose, there is a built-in function <code>panic</code>
3401that in effect creates a run-time error that will stop the program
3402(but see the next section). The function takes a single argument
3403of arbitrary type&mdash;often a string&mdash;to be printed as the
3404program dies. It's also a way to indicate that something impossible has
3405happened, such as exiting an infinite loop.
3406</p>
3407
3408
3409<pre>
3410// A toy implementation of cube root using Newton's method.
3411func CubeRoot(x float64) float64 {
3412 z := x/3 // Arbitrary initial value
3413 for i := 0; i &lt; 1e6; i++ {
3414 prevz := z
3415 z -= (z*z*z-x) / (3*z*z)
3416 if veryClose(z, prevz) {
3417 return z
3418 }
3419 }
3420 // A million iterations has not converged; something is wrong.
3421 panic(fmt.Sprintf("CubeRoot(%g) did not converge", x))
3422}
3423</pre>
3424
3425<p>
3426This is only an example but real library functions should
3427avoid <code>panic</code>. If the problem can be masked or worked
3428around, it's always better to let things continue to run rather
3429than taking down the whole program. One possible counterexample
3430is during initialization: if the library truly cannot set itself up,
3431it might be reasonable to panic, so to speak.
3432</p>
3433
3434<pre>
3435var user = os.Getenv("USER")
3436
3437func init() {
3438 if user == "" {
3439 panic("no value for $USER")
3440 }
3441}
3442</pre>
3443
3444<h3 id="recover">Recover</h3>
3445
3446<p>
3447When <code>panic</code> is called, including implicitly for run-time
3448errors such as indexing a slice out of bounds or failing a type
3449assertion, it immediately stops execution of the current function
3450and begins unwinding the stack of the goroutine, running any deferred
3451functions along the way. If that unwinding reaches the top of the
3452goroutine's stack, the program dies. However, it is possible to
3453use the built-in function <code>recover</code> to regain control
3454of the goroutine and resume normal execution.
3455</p>
3456
3457<p>
3458A call to <code>recover</code> stops the unwinding and returns the
3459argument passed to <code>panic</code>. Because the only code that
3460runs while unwinding is inside deferred functions, <code>recover</code>
3461is only useful inside deferred functions.
3462</p>
3463
3464<p>
3465One application of <code>recover</code> is to shut down a failing goroutine
3466inside a server without killing the other executing goroutines.
3467</p>
3468
3469<pre>
3470func server(workChan &lt;-chan *Work) {
3471 for work := range workChan {
3472 go safelyDo(work)
3473 }
3474}
3475
3476func safelyDo(work *Work) {
3477 defer func() {
3478 if err := recover(); err != nil {
3479 log.Println("work failed:", err)
3480 }
3481 }()
3482 do(work)
3483}
3484</pre>
3485
3486<p>
3487In this example, if <code>do(work)</code> panics, the result will be
3488logged and the goroutine will exit cleanly without disturbing the
3489others. There's no need to do anything else in the deferred closure;
3490calling <code>recover</code> handles the condition completely.
3491</p>
3492
3493<p>
3494Because <code>recover</code> always returns <code>nil</code> unless called directly
3495from a deferred function, deferred code can call library routines that themselves
3496use <code>panic</code> and <code>recover</code> without failing. As an example,
3497the deferred function in <code>safelyDo</code> might call a logging function before
3498calling <code>recover</code>, and that logging code would run unaffected
3499by the panicking state.
3500</p>
3501
3502<p>
3503With our recovery pattern in place, the <code>do</code>
3504function (and anything it calls) can get out of any bad situation
3505cleanly by calling <code>panic</code>. We can use that idea to
3506simplify error handling in complex software. Let's look at an
3507idealized version of a <code>regexp</code> package, which reports
3508parsing errors by calling <code>panic</code> with a local
3509error type. Here's the definition of <code>Error</code>,
3510an <code>error</code> method, and the <code>Compile</code> function.
3511</p>
3512
3513<pre>
3514// Error is the type of a parse error; it satisfies the error interface.
3515type Error string
3516func (e Error) Error() string {
3517 return string(e)
3518}
3519
3520// error is a method of *Regexp that reports parsing errors by
3521// panicking with an Error.
3522func (regexp *Regexp) error(err string) {
3523 panic(Error(err))
3524}
3525
3526// Compile returns a parsed representation of the regular expression.
3527func Compile(str string) (regexp *Regexp, err error) {
3528 regexp = new(Regexp)
3529 // doParse will panic if there is a parse error.
3530 defer func() {
3531 if e := recover(); e != nil {
3532 regexp = nil // Clear return value.
3533 err = e.(Error) // Will re-panic if not a parse error.
3534 }
3535 }()
3536 return regexp.doParse(str), nil
3537}
3538</pre>
3539
3540<p>
3541If <code>doParse</code> panics, the recovery block will set the
3542return value to <code>nil</code>&mdash;deferred functions can modify
3543named return values. It will then check, in the assignment
3544to <code>err</code>, that the problem was a parse error by asserting
3545that it has the local type <code>Error</code>.
3546If it does not, the type assertion will fail, causing a run-time error
3547that continues the stack unwinding as though nothing had interrupted
3548it.
3549This check means that if something unexpected happens, such
3550as an index out of bounds, the code will fail even though we
3551are using <code>panic</code> and <code>recover</code> to handle
3552parse errors.
3553</p>
3554
3555<p>
3556With error handling in place, the <code>error</code> method (because it's a
3557method bound to a type, it's fine, even natural, for it to have the same name
3558as the builtin <code>error</code> type)
3559makes it easy to report parse errors without worrying about unwinding
3560the parse stack by hand:
3561</p>
3562
3563<pre>
3564if pos == 0 {
3565 re.error("'*' illegal at start of expression")
3566}
3567</pre>
3568
3569<p>
3570Useful though this pattern is, it should be used only within a package.
3571<code>Parse</code> turns its internal <code>panic</code> calls into
3572<code>error</code> values; it does not expose <code>panics</code>
3573to its client. That is a good rule to follow.
3574</p>
3575
3576<p>
3577By the way, this re-panic idiom changes the panic value if an actual
3578error occurs. However, both the original and new failures will be
3579presented in the crash report, so the root cause of the problem will
3580still be visible. Thus this simple re-panic approach is usually
3581sufficient&mdash;it's a crash after all&mdash;but if you want to
3582display only the original value, you can write a little more code to
3583filter unexpected problems and re-panic with the original error.
3584That's left as an exercise for the reader.
3585</p>
3586
3587
3588<h2 id="web_server">A web server</h2>
3589
3590<p>
3591Let's finish with a complete Go program, a web server.
3592This one is actually a kind of web re-server.
3593Google provides a service at
3594<a href="http://chart.apis.google.com">http://chart.apis.google.com</a>
3595that does automatic formatting of data into charts and graphs.
3596It's hard to use interactively, though,
3597because you need to put the data into the URL as a query.
3598The program here provides a nicer interface to one form of data: given a short piece of text,
3599it calls on the chart server to produce a QR code, a matrix of boxes that encode the
3600text.
3601That image can be grabbed with your cell phone's camera and interpreted as,
3602for instance, a URL, saving you typing the URL into the phone's tiny keyboard.
3603</p>
3604<p>
3605Here's the complete program.
3606An explanation follows.
3607</p>
3608{{code "/doc/progs/eff_qr.go" `/package/` `$`}}
3609<p>
3610The pieces up to <code>main</code> should be easy to follow.
3611The one flag sets a default HTTP port for our server. The template
3612variable <code>templ</code> is where the fun happens. It builds an HTML template
3613that will be executed by the server to display the page; more about
3614that in a moment.
3615</p>
3616<p>
3617The <code>main</code> function parses the flags and, using the mechanism
3618we talked about above, binds the function <code>QR</code> to the root path
3619for the server. Then <code>http.ListenAndServe</code> is called to start the
3620server; it blocks while the server runs.
3621</p>
3622<p>
3623<code>QR</code> just receives the request, which contains form data, and
3624executes the template on the data in the form value named <code>s</code>.
3625</p>
3626<p>
3627The template package <code>html/template</code> is powerful;
3628this program just touches on its capabilities.
3629In essence, it rewrites a piece of HTML text on the fly by substituting elements derived
3630from data items passed to <code>templ.Execute</code>, in this case the
3631form value.
3632Within the template text (<code>templateStr</code>),
3633double-brace-delimited pieces denote template actions.
3634The piece from <code>{{html "{{if .}}"}}</code>
3635to <code>{{html "{{end}}"}}</code> executes only if the value of the current data item, called <code>.</code> (dot),
3636is non-empty.
3637That is, when the string is empty, this piece of the template is suppressed.
3638</p>
3639<p>
3640The two snippets <code>{{html "{{.}}"}}</code> say to show the data presented to
3641the template—the query string—on the web page.
3642The HTML template package automatically provides appropriate escaping so the
3643text is safe to display.
3644</p>
3645<p>
3646The rest of the template string is just the HTML to show when the page loads.
3647If this is too quick an explanation, see the <a href="/pkg/html/template/">documentation</a>
3648for the template package for a more thorough discussion.
3649</p>
3650<p>
3651And there you have it: a useful web server in a few lines of code plus some
3652data-driven HTML text.
3653Go is powerful enough to make a lot happen in a few lines.
3654</p>
3655
3656<!--
3657TODO
3658<pre>
3659verifying implementation
3660type Color uint32
3661
3662// Check that Color implements image.Color and image.Image
3663var _ image.Color = Black
3664var _ image.Image = Black
3665</pre>
3666-->