logo资料库

Level Up Your Web Apps With Go.pdf

第1页 / 共258页
第2页 / 共258页
第3页 / 共258页
第4页 / 共258页
第5页 / 共258页
第6页 / 共258页
第7页 / 共258页
第8页 / 共258页
资料共258页,剩余部分请下载后查看
Level Up Your Web Apps With Go
Table of Contents
Preface
Who Should Read This Book
Conventions Used
Code Samples
Tips, Notes, and Warnings
Supplementary Materials
Some Notes
HTTP Requests
Go Get
Formatting
Imports
Guiding Gently, Not Explicitly
Terminal
Want to take your learning further?
Welcome New Gopher
Installation
The Go Workspace
Mac OS and Linux
Windows
Your First Go Code
Go Tools
Basic Types
Strings
Numbers
Integers
Floats
Booleans
Arrays and Slices
Looping
Maps
Looping over Maps
Dealing with Map Values
Functions
Pointers
Structs
Type Methods
Exported and Unexported Code
Summary
Go Types Explored
Custom Types
Interfaces
Error Handling
Embedded Types
The defer Command
Third-party Libraries
Syntax Options and Conventions
Alternative Syntax Options
Initializing Structs without Field Names
Empty Variable Initialization
Trailing Commas
Coding Style Conventions
Variable Naming
Constants
Constructors
Summary
HTTP
Responding to Requests
Breaking It Down
Adding More Information
Pattern Matching in Depth
Returning Errors
The Handler Interface
Chaining Handlers to Create Middleware
HTML Templates
Accessing Data
Conditionals with if and else
Loops with Range
Multiple Templates
Pipelines
Variables
Rendering JSON
Marshaling
Marshaling Structs
Custom JSON Keys
Nested Types
Unmarshaling Types
Unknown JSON Structure
Summary
Gophr Part 1: The Project
Using Bootstrap
Project Layout
Serving Assets
Rendering Pages
Creating a Layout
Advanced Routing
Using httprouter
Flexible Middleware
How Routing Works with Our Middleware
Summary
Gophr Part 2: All About the Users
Before We Start
What do users look like?
Hashing a Password
Identifiers
User Forms
Creating Users
Registration Form
Registration Handler
Creating Users
Input Validation
Persisting Users
Summary
Exercises
Gophr Part 3: Remembering Our Users
What makes up a session?
Persisting User Sessions
Checking for a User
Displaying User Information
Signing Out, Signing In
The Sign Out Process
The Login Process
Editing Your Details
Summary
Exercises
Gophr Part 4: Images
The Image Type
ImageStore Interface
Getting My(SQL) Groove On
MySQL Requirements
Connecting to Databases
Creating ImageStore
Implementing ImageStore in DBImageStore
Save
Find
FindAll
FindAllByUser
Uploading Images
Creating Images from a URL
Creating Images from a File
Displaying Images
Showing the Front Page Images
Showing a Single Image
Showing a User’s Images Only
Summary
Exercises
Gophr Part 5: Concurrency
Goroutines
Waiting for Goroutines to Finish
Communicating with Goroutines
Communicating with Multiple Channels
Using Channels and Selects for Timeouts
Looping on selects
Throwing Away Goroutines
Putting Goroutines into Practice
Using the Resized Images
Summary
Automated Testing
Writing Tests in Go
Passing and Failing
Testing Multiple Variations of Inputs
Code Coverage
Testing Between Packages
Testing HTTP Requests and Responses
Testing the Gophr Authentication
Testing Remote HTTP requests
Performance Benchmarking
Benchmark Regressions
Summary
Exercises
Packaging and Production
Creating Packages
What is a package?
An Example Package
Exporting
Avoiding Circular Imports
Let’s Implement a Real Package
Package Exercise
Dependency Management
Godep
Saving Your Dependencies
Using Dependencies
The gopkg.in Service
Deploying Go Applications
Compiling on the Server
Compiling Elsewhere
Cross-compiling
Building Go for Other Platforms
Mac OS and Linux
Windows
Building Binaries for Other Platforms
That’s All, Folks!
Summary of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv 1. Welcome New Gopher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2. Go Types Explored . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3. HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4. Gophr Part 1: The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5. Gophr Part 2: All About the Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 6. Gophr Part 3: Remembering Our Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 7. Gophr Part 4: Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 8. Gophr Part 5: Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 9. Automated Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 10. Packaging and Production . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
LEVEL UP YOUR WEB APPS WITH GO BY MAL CURTIS
iv Level Up Your Web Apps With Go by Mal Curtis Copyright © 2015 SitePoint Pty. Ltd. Product Manager: Simon Mackie Technical Editor: Lionel Barrow English Editor: Kelly Steele Cover Designer: Alex Walker Notice of Rights All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews. Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein. Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark. Published by SitePoint Pty. Ltd. 48 Cambridge Street Collingwood VIC Australia 3066 Web: www.sitepoint.com Email: business@sitepoint.com ISBN 978-0-9924612-9-4 (print) ISBN 978-0-9941826-0-9 (ebook) Printed and bound in the United States of America
v About Mal Curtis Mal Curtis is a Kiwi polyglot software engineer currently focusing on Go and JavaScript. He’s the founder of transactional email management service Apostle.io, and is a Principal Engineer at Vend, where he helps make beautiful Point of Sale and Inventory Management software (yes, it can be beautiful). In the past he’s helped launch Learnable.com, and worked for SitePoint in Melbourne. In his spare time you’ll find him attempting geeky pursuits with varying levels of failure, such as quadcopters and sous vide cooking. About SitePoint SitePoint specializes in publishing fun, practical, and easy-to-understand content for web professionals. Visit http://www.sitepoint.com/ to access our blogs, books, newsletters, articles, and community forums. You’ll find a stack of information on JavaScript, PHP, Ruby, mobile development, design, and more.
To my Dad — for surrounding me with technology from my earliest days, I can never thank you enough. I miss you. And to Sarah — for your unrelenting support and patience. You’re my rock.
分享到:
收藏