Example: biology

The Go Programming Language - beyondkmp

Ptg16105617 From the Library of YIGUANG HUptg16105617 The Go Programming LanguageFrom the Library of YIGUANG HUptg16105617 This page intentionally left blank From the Library of YIGUANG HUptg16105617 The Go Programming LanguageAlan A. A. DonovanGoogle W. KernighanPrinceton UniversityNew York Boston Indianapolis San FranciscoToronto Montreal London Munich Paris MadridCapetown Sydney Tokyo Singapore Mexico CityFrom the Library of YIGUANG HUptg16105617 Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trade-mark claim, the designations have been printed with initial capital letters or in all e authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions.

Go isanopen-s ource pro jec t,sosourcecodefor itscompi ler,librar ies, andtools is fre ely avai l-able toany one.Contr ibution s to the pro jec t come fro m an active worldw ide community.Go runs onUnix-li kesystems—Linux, Fre eBSD, OpenBSD, Mac OS …

Tags:

  Librar ies, Librar

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of The Go Programming Language - beyondkmp

1 Ptg16105617 From the Library of YIGUANG HUptg16105617 The Go Programming LanguageFrom the Library of YIGUANG HUptg16105617 This page intentionally left blank From the Library of YIGUANG HUptg16105617 The Go Programming LanguageAlan A. A. DonovanGoogle W. KernighanPrinceton UniversityNew York Boston Indianapolis San FranciscoToronto Montreal London Munich Paris MadridCapetown Sydney Tokyo Singapore Mexico CityFrom the Library of YIGUANG HUptg16105617 Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trade-mark claim, the designations have been printed with initial capital letters or in all e authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions.

2 No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, train-ing goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) government sales inquiries, please contact For questions about sales outside the United States, please contact Visit us on the Web: of Congress Control Number: 2015950709 Copyright 2016 Alan A. A. Donovan & Brian W. KernighanAll rights reserved. Printed in the United States of America. Th is publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.

3 To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, 200 Old Tappan Road, Old Tappan, New Jersey 07675, or you may fax your request to (201) cover: Millau Viaduct, Tarn valley, southern France. A paragon of simplicity in modern engi-neering design, the viaduct replaced a convoluted path from capital to coast with a direct route over the clouds. Jean-Pierre cover: the original Go gopher. 2009 Ren e French. Used under Creative Commons Attribu-tions p e s e t by t h e aut h ors i n Mi n i on Pro, L ato, an d C ons ol a s , u s i ng G o, g roff , ghostscript, and a host of other open-source Unix tools. Figures were created in Google : 978-0-13-419044-0 ISBN-10: 0-13-419044-0 Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, printing, October 2015 From the Library of YIGUANG HUptg16105617Fo rLeil aan dMe gThe Go Programming Language 2016 Alan A.

4 A. Donovan & Brian W. Kernighanrevision 3b600c, date 29 Sep 2015 From the Library of YIGUANG HUptg16105617 This page intentionally left blank From the Library of YIGUANG HUptg16105617 Cont entsPr eface xiTheOrigins ofGoxiiTheGoPro jec txiiiOrganizat ionofthe BookxvWh ere toFindMoreInfor mat ionxviAc knowledgments xvii1. Hel lo, Wor Command-L ineArguments FindingDup lic ateLines Animated GIFs FetchingaURL FetchingURLs Con cur AWe bServer Loose End s232. Pro gramStr Names Declarat ions Var iables Assig nments Typ eDecl arat ions Packages andFiles Scope 45viiThe Go Programming Language 2016 Alan A. A. Donovan & Brian W. Kernighanrevision 3b600c, date 29 Sep 2015 From the Library of YIGUANG HUptg16105617viii CONTENTS3. Basic Data Typ Int egers Float ing-Point Numbers Complex Numbers Boole Str Con stants754.

5 Com positeTyp Arrays Slices Maps Str Text andHTMLT empl ates1135. Func tions FunctionDeclarat ions Rec MultipleRetur nValues Erro FunctionValues Anony mou sFunc tions Var iadic Functions Defer red FunctionCal Panic Recov er1516. Metho Met hod Declarat ions Met hodswit haPoint ComposingTyp esby Str uct Emb Met hod Values andExpressions Example: Bit Vec tor Typ Encapsulat ion1687. Int erfaces as Contrac Int erface Typ Int erface Satisfac ParsingFlags wit Int erface Values 181 The Go Programming Language 2016 Alan A. A. Donovan & Brian W. Kernighanrevision 3b600c, date 29 Sep 2015 From the Library of YIGUANG Sor tingwit TheerrorInterface Example: Typ eAs ser tions Discriminat ingError swith Typ eAs ser tions Quer yingBeh avior swith Int erface Typ eAs ser tions Typ eSw itch Example: Token-B ased XML AFe wWo rds ofAdv ice 2168.

6 Gor o utines and Channels Example: Con cur rentClo ckSer ver Example: Con cur rentEch oServer Channel Looping inParal Example: Con cur rentWeb Craw Multiplexingwit Example: Con cur rentDirec tor Cancellat Example: ChatSer ver 2539. Concurrency withShared Vari Race Con dit ion Mut ual Exc Read/Write Mut Memor ySy nchro nizat Lazy Initializat TheRace Detec tor Example: Con cur rentNon-Blo cking Cache Goroutinesand Threads 28010. Pack agesand the Int roduc Imp ort Pat ThePackageDeclarat Imp ort Declarat ions Blank Imp Packages TheGoTool290 The Go Programming Language 2016 Alan A. A. Donovan & Brian W. Kernighanrevision 3b600c, date 29 Sep 2015 From the Library of YIGUANG HUptg16105617xCONTENTS11. Testing Thego testTo tions Cov tions tions 32612.

7 Reflecti Why Reflec tion? ,aRec ursiveValue Print Example: Enco dingS-E xpressions Setting Var iables wit Example: DecodingS-E xpressions AccessingStr uct Field Tags Displaying the Met hodsofaTyp AWo rdofCaution 35213. Low-L evel Pro ,Alignof, Example: DeepEquivalence Cal lingCCodewit Another WordofCaution 366 Index367 The Go Programming Language 2016 Alan A. A. Donovan & Brian W. Kernighanrevision 3b600c, date 29 Sep 2015 From the Library of YIGUANG HUptg16105617 Pref ace Go is anopensourc eprog rammi n glang uagethatmak esiteasytobui ldsimpl e,rel iab le,an defficientsof tware. (Fro mtheGoweb sit )Go was conceive din Septemb er2007 byRob ert Gries emer,Rob Pike, and Ken Thomps on, allat Google, and was announced inNov emb er2009. Thegoals ofthe langu ageand its accom-pany ing tools weretobeexpressive, efficient in bot hcomp ilation and exe cut ion,and effec tivein writing reliableand bears a sur facesimi lar ity toC and,likeC,isatoolfor prof essionalprogrammers, achie v-ingmaximum effe ctwit hminimum it ismuchmorethananupdated versionofC.

8 Itbor rowsand adaptsgood ide asfrom manyother langu ages, whi le avoidingfeaturesthathave led tocomplexity and unreliablecode. Its facilities for con cur rency are new and efficient,andits approach to dat a abstrac tionand obj e ct-oriente dprog rammingisunu sually flexible. Ithasaut omat icmemory managementorgarb age col lec isesp eci ally wel lsuit edfor bui ldinginf rastr ucturelikenet wor ked ser vers, andtools andsystems for prog rammers, but it istruly a general-pur pos elanguage andfind sus ein domainsas divers eas graphics, mobileapp lic ations,and machinelearning. Ithas becom epopu lar as arepl acementfor unt ypedscr ipt ing langu ages because itbal ances expressivenesswit hsafety :Go programstypic ally run fasterthanprogramswritt enindynamic langu ages andsuf fer farfe wer crashesdue tounexp ected typ eer ror isanopen-s ource pro jec t,sosourcecodefor itscompi ler, librar ies, andtools is fre ely avai l-able toany ibution sto the pro jec tcome fro man active worldw ide onUnix-li kesystems Linux, Fre eBSD, OpenBSD, Mac OS X andonPlan9andMicros oft Windows.

9 Programswritt eninone ofthese env iro nmentsgeneral lywor kwithoutmo dification on the Go Programming Language 2016 Alan A. A. Donovan & Brian W. Kernighanrevision 3b600c, date 29 Sep 2015 From the Library of YIGUANG HUptg16105617xii PREFACET hisbookismeant tohelpyou start usingGoeffec tive lyrig htaway andtouse itwel l,tak ingfull advantage ofGo slangu agefeaturesand stand ard librar iesto write clear, idiomatic, ofGoLike biolog ical species, successf ullangu ages beget offspring thatincor poratethe advantagesoftheir ancestors;int erbre e dingsom etimesleads tosur prisingstrengt hs; and, ver yoccasionally,aradic alne wfe ature ariseswit houtpre cedent. Wecan lear nalot about why a langu ageistheway itisand whatenv iro nment ithas beenadapt edfor bylooking atthese figurebelow shows the most imp ortantinfluences ofearlier programminglangu ages onthedesig nof issom etimesdes crib edasa C-li kelangu age, or as Cfor the 21stcentury.

10 Fr omC,Goin her ite ditsexpressionsyntax, cont rol-flow statements, basic dat atypes, cal l-by-value param-eter passing, point ers,and above all,C semp hasisonprogramsthatcompi letoefficientmachinecodeand cooperatenatural lywit htheabstrac tions ofcur rentoperat Go Programming Language 2016 Alan A. A. Donovan & Brian W. Kernighanrevision 3b600c, date 29 Sep 2015 From the Library of YIGUANG HUptg16105617 THE ORIGINS OF GOxiiiButthere are other ancestors inGo sfami lytre maj orstreamofinfluence comesfro mlanguagesbyNik lausWir th, beg inningwit inspired the packagecon ero neliminated the distinc tionbet weenmoduleint erface files andmoduleimp lementationfiles. Obero n-2 influence dthesyntaxfor packages, imports, anddeclarat ions,and Obj e ctOb ero nprov ide dthesyntaxfor met hod declarat her lineage among Go sancestors,and one thatmakes Godistinc tiveamong recentprog ramminglangu ages, isa sequence oflit tle-k now nresearch langu ages deve lop edatBel lLabs, allins pired bythe con ceptofcommu nicating sequent ial pro cesses(CSP) fro mTo nyHo are sseminal 1978 pap eron the found ation sof con cur ,aprogram isaparal lelcomp osition of processesthathavenoshare dst ate;the pro cessescommunic ateand synchro-nize usingchannel Hoare sCSP was a for mal langu agefor des cribingthe fundament alconcepts ofcon cur rency,not a programminglangu agefor writing exe cut othersbegan toexp erimentwit hCSPimp lementation sas actu allangu ages.


Related search queries