effective_go.html 114 KB

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