Web Development with Go(pdf)

所需积分/C币:10 2016-05-10 15:29:04 4.23MB PDF
8
收藏 收藏
举报

Go, often referred to as Golang, is a general-purpose programming language that was developed at Google in November 2009. Several programming languages are available for writing different kinds of software systems, and some languages have existed for decades. Some mainstream programming languages ar
Web Development with Go Copyright 2015 by Shiju Varghese This work is subject to copyright All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information torage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher's location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through Rightslink at the Copyright Clearance Center Violations are liable to prosecution under the respective Copyright Law ISBN-13(pbk):978-1-4842-1053-6 ISBN-13( electronic:978-1-4842-10529 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights While the advice and information in this book are believed to be true and accurate at the date of publicatio neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein Managing director: Welmoed spahr Lead Editor: Celestin John Suresh Technical reviewer: Prateek baheti Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey pepper son Editorial Board: Steve Anglin, Louise Corrigan, Jim DeWolf, Jonathan Gennick, Robert Hutchir Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing Coordinating Editor: Jill Balzano Copy Editor Nancy Sixsmith Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Distributed to the book trade worldwide by Springer Science+ Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax(201)348-4505 e-mailorders-ny@springer-sbm.comorvisitwww.springer.comApressMedia,lLcisaCaliforniaLlc and the sole member(owner)is Springer Science+ Business Media Finance Inc(SSBM Finance Inc) SSBM Finance Inc is a Delaware corporation Forinformationontranslationspleasee-mailrights@apress.com,orvisitwww.apress.com Apress and friends ofed books may be purchased in bulk for academic, corporate or promotional use eBook versions and licenses are also available for most titles. For more information, reference our Special BulkSales-ebookLicensingwebpageatwww.apress.com/bulk-sales Any source code or other supplementary material referenced by the author in this text is available to readers atwww.apress.comFordetailedinformationabouthowtolocateyourbookssourcecodegoto www.apress.com/source-code/. www.it-ebooks.info I would like to dedicate this book to my parents, the late C.s. varghese and rosy varghese. I would like to thank them for their unconditional love and life struggles for the betterment ofour lives Without their love and support, this book would not have been possible ene rose I would like to dedicate this book to my lovely wife rosmi and beautiful daughter Finally, i would like to dedicate this book to my elder sister Shaijy and younger brother shinto -Shiju varghese www.it-ebooks.info Contents at a glance About the author About the technical reviewer ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ XVII Introduction ■■■■■■■■■■■ XIX Chapter 1: Getting started with Go ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Chapter 2: Go Fundamentals aa ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 15 Chapter3:User- Defined Types and concurrency…,,,,,,……mm35 Chapter 4: Getting Started with Web Development mmmm mmn 59 Chapter 5: Working with Go Templates ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 79 Chapter 6: Http Middleware ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 99 Chapter 7: Authentication to Web Apps.aan 121 Chapter 8: Persistence with MongoDB mmmmmmmmmmmmmmmmmm 141 Chapter 9: Building restful Services ma. ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 159 Chapter 10: Testing Go Applications ammar 211 Chapter 11: Building Go Web applications on Google Cloud mmmmmammammmmmnan 251 Index 285 www.it-ebooks.info Contents About the author About the technical reviewer mmmxvif Introduction ■■■■■■■■■■ Chapter 1: Getting Started with Go ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Introducing go Minimalistic Language with Pragmatic Design A Static Type Language with High Productivity 日日日日日日自日面日日面日日自日日自日面日日日日日日自日面日日日日自目日自重日面 2 Concurrency Is a Built-In Feature at the Language Level 2 Go Compiles Programs Quickly................................ Go as a General-Purpose Language....................................3 Go Ecosystem Installing the go tools Checking the Installation Setting up a Work environment. 7 G0 Workspace....…7 GOPATH Environment variable Code organization Paths Writing Go Programs 8 Writing a hello world program…........,,………8 Writing a Library 日B日面B日面日日Ba自面日面日日面面日日面面日B日面日日面面日自日日面面日日日面面日自日日日面面日自日日自面日日自日面面日自日日面日日日面 Testing go code Using go playground 12 www.it-ebooks.info CONTENTS Using Go Mobile∴ 13 Go as a Language for Web and Microservices 13 Summary ,14 Chapter 2: Go Fundamentals ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■口■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 15 Packages...............15 Package main 15 Package alias 16 Function init 16 Using a blank ldentifi 17 Importing Packages 8 Install Third-Party Packages.........................................18 Writing Packages Go Too 21 Formatting Go Code.….,,,,…, ,日面a面面面,日面面a日面面面面日面面量日面面面日B日面面 22 Go Documentation Working with Collections 24 rays…… 24 Slices 25 Maps 29 Defer panic and recover 31 Defer……………………………………… 31 Panic 32 Recover wwww 32 Error Handling… 33 Summary 34 Chapter 3: User-Defined Types and concurrency 35 User-defined Types with Structs 35 Creating a Struct Type .. Creating Instances of Struct Types 36 Adding Behavior to a Struct Type. www.it-ebooks.info CONTENT Type composition 40 Overriding Methods of Embedded type 43 Working with Interfaces ,44 Concurrency… 50 Goroutines 50 GOMAXPROCS and Parallelism Channels 54 Summary 58 Chapter4: Getting Started with Web Development,,,,,,,…,……59 net/httpPackage 59 Processing Http Requests 60 ServeMux… Hand|er,,……… Building a Static Web Server. 61 Creating Custom Handlers .63 Using Functions as Handlers.ammmmnannemmnnemannmannmann, 64 httpHandlerfunctype......m.........e............................................................................64 Servemuxhandlefuncfunctionwwwww.66 DefaultserveMux 66 httpServerStruct 67 Gorilla mux 69 Building a REStful APl 70 Data model and data store Configuring the Multiplexer…....,.,.,.,,.,… Handler Functions for CRUD Operations ….74 Summary… www.it-ebooks.info CONTENTS Chapter 5: Working with Go Templates mma maammmammmmaammmamm 79 text/template Package Working with text/template..,.,.,…..….9 Define Named templates …83 Declaring variables 83 Using Pipes........,...,,...,,.,,… 84 Building hTMl views Using html/template 84 Building a Web Application….............,,…,…….85 Summary… 97 Chapter6: Http MIddleware,,,,,,,,,,,……,…99 Introductiontohttpmiddleware Writing Http Middleware 100 Howtowritehttpmiddleware 101 Writing a Logging Middleware ControllingtheflowofhttpMiddleware.........m...........103 Using third-Party Middleware 106 Using Gorilla handlers. 106 Middleware Chaining with the Alice Package...............................108 Using middleware with the Negroni package 111 Getting Started with Negroni........…….11 Working with a Negroni Middleware Stack 115 Sharing values Among Middleware 118 Using Gorilla context 118 Setting and getting Values with Gorilla context 118 Summary… 120 Chapter 7: Authentication to Web Apps mamm 121 Authentication and authorization ,121 Authentication Approaches.... aaaaa. 121 Cookie-Based Authentication 122 Token -Based authentication 123 ww. it-ebooks info CONTENTS Authentication with oauth 2 125 Understanding oauth 2 125 Authentication with OAuth 2 using the Goth Package 126 Authentication with json Web token 131 Working with JWT Using the jwt-go Package...........131 Using Http Middleware to validate Jwt Tokens Summary 139 Chapter 8: Persistence with MongoDB ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 141 Introduction to MongoDB ,141 Getting Started Using MongoDB ……………142 Introduction to mgo Driver for MongoDB 142 Accessing Collections…......,,…,…,…,……144 CRUD Operations with MongoDB 144 Inserting Documents…..,......,,….,144 Reading documents 149 Updating Documents… 151 Deleting documents Indexes in MongoDB 152 Managing Sessions …154 Summary 157 Chapter 9: Building restful services n. RESTful APIs: the Backbone of Digital Transformation 159 APl-Driven Development with RESTful APls 160 Go: the great stack for restful services∴…160 Go: the great stack for Building RESTful APls 162 Third-Party packages 62 Application Structure... ……162 Data model 164 Resource Modeling for RESTful APls 重日a日日日B日日面日日日面口日自日面日日日面日日自日面日日自日面日日自日日日自日自日日自日口 165 www.it-ebooks.info

...展开详情
试读 127P Web Development with Go(pdf)
立即下载 低至0.43元/次 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
上传资源赚积分or赚钱
    最新推荐
    Web Development with Go(pdf) 10积分/C币 立即下载
    1/127
    Web Development with Go(pdf)第1页
    Web Development with Go(pdf)第2页
    Web Development with Go(pdf)第3页
    Web Development with Go(pdf)第4页
    Web Development with Go(pdf)第5页
    Web Development with Go(pdf)第6页
    Web Development with Go(pdf)第7页
    Web Development with Go(pdf)第8页
    Web Development with Go(pdf)第9页
    Web Development with Go(pdf)第10页
    Web Development with Go(pdf)第11页
    Web Development with Go(pdf)第12页
    Web Development with Go(pdf)第13页
    Web Development with Go(pdf)第14页
    Web Development with Go(pdf)第15页
    Web Development with Go(pdf)第16页
    Web Development with Go(pdf)第17页
    Web Development with Go(pdf)第18页
    Web Development with Go(pdf)第19页
    Web Development with Go(pdf)第20页

    试读结束, 可继续阅读

    10积分/C币 立即下载 >