ebook img

Webapps in Go PDF

192 Pages·00.889 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Webapps in Go

Webapps in Go the anti textbook Suraj Patil Thisbookisforsaleathttp://leanpub.com/antitextbookGo Thisversionwaspublishedon2018-06-09 ThisisaLeanpubbook.LeanpubempowersauthorsandpublisherswiththeLeanPublishing process.LeanPublishingistheactofpublishinganin-progressebookusinglightweighttoolsand manyiterationstogetreaderfeedback,pivotuntilyouhavetherightbookandbuildtractiononce youdo. ©2016-2018SurajPatil Tweet This Book! PleasehelpSurajPatilbyspreadingthewordaboutthisbookonTwitter! Thesuggestedtweetforthisbookis: IjustboughtWebappsinGoby@igeeko. Thesuggestedhashtagforthisbookis#WebDevGoAntiTextbook. Findoutwhatotherpeoplearesayingaboutthebookbyclickingonthislinktosearchforthis hashtagonTwitter: #WebDevGoAntiTextbook AkshayAgarwal:ForintroducingmetotheFOSSworld. HrishikeshNandedkar:Forguidingmeintherightdirection. Contents Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Systemrequirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 InstalltheGotools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Thetoolchain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Hello,Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 WhyGo? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Variables&Datastructures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Underlyingdatastructures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Importantpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 array,slice,map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 ControlstatementsandFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Controlstatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 BasicsofStruct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Object-oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 ManagingtheWorkspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Internaldeployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 CONTENTS WebProgrammingBasics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 HTTPMethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Whatisatemplate? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 StaticFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Basicwebapplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 HandlingURLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Servingstaticfiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Homework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Designingourwebapp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 TheDesign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Homework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 UsingdatabasesinGo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Creatingandconfiguringdatabase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Installingsqlitedriver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Accessingthedatabase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Importingdriver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 RetrievingResultSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 FetchingDatafromtheDatabase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 HowScan()Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 PreparingQueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Single-RowQueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 ModifyingDataandUsingTransactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 StatementsthatModifyData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 WorkingwithTransactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 UsingPreparedStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 PreparedStatementsAndConnections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 AvoidingPreparedStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 PreparedStatementsinTransactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 ParameterPlaceholderSyntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 HandlingErrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 ErrorsFromIteratingResultsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 ErrorsFromClosingResultsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 ErrorsFromQueryRow() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 IdentifyingSpecificDatabaseErrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 HandlingConnectionErrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 CONTENTS WorkingwithNULLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 WorkingwithUnknownColumns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Theconnectionpool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Surprises,AntipatternsandLimitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 ResourceExhaustion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Largeuint64Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 ConnectionStateMismatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Database-SpecificSyntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 MultipleResultSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 InvokingStoredProcedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 MultipleStatementSupport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 DatabaseEncapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 AnExample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Homework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 WorkingwithForms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 CSRF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 InputValidation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Uploadingfiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Subtemplating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Homework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Middlewares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 BuildinganAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 JWT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 MakinganAPIcall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 CONTENTS FormattingaJSONdocument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 TestingAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Writinganclient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Gettingthetoken . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Errorhandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 AdvancedUsage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 UnitTesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 VersionControlBasics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Usinggit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Remote . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Installation IfyouknowaboutinstallationorhaveinstalledGo,youcanskiptoTools¹. Thischapteristakenfrominstallpage²verbatim,exceptforthechangestobemadetoadapttothis book’sstylingformat. System requirements Gobinarydistributionsareavailableforthesesupportedoperatingsystemsandarchitectures.Please ensure your system meets these requirements before proceeding. If your OS or architecture is not onthelist,youmaybeabletoinstallfromsourceorusegccgoinstead Operatingsystem Architectures Notes FreeBSD8-STABLEorlater amd64 DebianGNU/kFreeBSDnot supported Linux2.6.23orlaterwithglibc amd64,386,arm CentOS/RHEL5.xnot supported;installfromsource forARM MacOSX10.7orlater amd64 usetheclangorgcc†that comeswithXcode‡ WindowsXPorlater amd64,386 useMinGWgcc†.Noneedfor cygwinormsys †gccisrequiredonlyifyouplantousecgo. ‡You only need to install the command line tools for Xcode. If you have already installed Xcode 4.3+,youcaninstallitfromtheComponentstaboftheDownloadspreferencespanel. Install the Go tools IfyouareupgradingfromanolderversionofGoyoumustfirstremovetheexistingversion.Linux, MacOSX,andFreeBSDtarballs Download the archive and extract it into /usr/local, creating a Go tree in /usr/local/go. For example: tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz ¹0.1tools.md ²https://golang.org/doc/install Installation 2 Choosethearchivefileappropriateforyourinstallation.Forinstance,ifyouareinstallingGoversion 1.2.1for64-bitx86onLinux,thearchiveyouwantiscalledgo1.2.1.linux-amd64.tar.gz. (Typicallythesecommandsmustberunasrootorthroughsudo.) Add /usr/local/go/bintothePATHenvironmentvariable.Youcandothisbyaddingthislineto your/etc/profile(forasystem-wideinstallation)or$HOME/.profile: export PATH=$PATH:/usr/local/go/bin Installing to a custom location The Go binary distributions assume they will be installed in /usr/local/go (or c:\Go under Windows), but it is possible to install the Go tools to a different location. In this case you must settheGOROOTenvironmentvariabletopointtothedirectoryinwhichitwasinstalled. For example, if you installed Go to your home directory you should add the following commands to$HOME/.profile: export GOROOT=$HOME/go export PATH=$PATH:$GOROOT/bin Note:GOROOTmustbesetonlywheninstallingtoacustomlocation. Mac OS X package installer Download the package file, open it, and follow the prompts to install the Go tools. The package installstheGodistributionto/usr/local/go. The package should put the /usr/local/go/bin directory in your PATH environment variable. YoumayneedtorestartanyopenTerminalsessionsforthechangetotakeeffect. Windows TheGoprojectprovidestwoinstallationoptionsforWindowsusers(besidesinstallingfromsource): aziparchivethatrequiresyoutosetsomeenvironmentvariablesandanMSIinstallerthatconfigures yourinstallationautomatically. MSI installer Open the MSI file and follow the prompts to install the Go tools. By default, the installer puts the Godistributioninc:Go. Theinstallershouldputthec:\Go\bindirectoryinyourPATHenvironmentvariable.Youmayneed torestartanyopencommandpromptsforthechangetotakeeffect.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.