Level Up Your Web Apps With Go (Mal Curtis) (z-library.sk, 1lib.sk, z-lib.sk)

Author: Mal Curtis

GO

The Internet is a place of constant evolution and creation. Nearly every day, web developers have new tools available to add to their repertoire. In recent years, though, few have been as influential as the Go programming language. Originally created at Google to solve system administration problems, Go has evolved into a modern, powerful, and well-adopted language. To call Go just a language would be a disservice, however; Go is more than a language—it’s an entire ecosystem. From the tools that come with it, to the community of developers that build on it, Go is a force majeure in the web development world and it is here to stay. So what is Go? Well, it’s a combination of a strongly typed programming language, and a collection of tools that make working with the language a pleasure. While many may consider a language to comprise merely the syntax, the tools provided to aid development are just as important—if not more so—as the language itself. I hope that as you work through this book you’ll see this for yourself, and learn to love Go as I do. The topics covered in this book are targeted at web development. While Go wasn’t created as a “language for web development,” it was produced with such a powerful and diverse standard library that web developers have taken to the language in droves, with many organizations now having Go applications powering their services. In this book, we won’t be covering every aspect of the standard library; instead we'll be diving deeply into how to create fast, powerful, and maintainable web applications.

📄 File Format: PDF
💾 File Size: 3.7 MB
9
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
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
📄 Page 3
(This page has no text content)
📄 Page 4
LEVEL UP YOUR WEB APPS WITH GO BY MAL CURTIS
📄 Page 5
Level Up Your Web Apps With Go by Mal Curtis Copyright © 2015 SitePoint Pty. Ltd. English Editor: Kelly SteeleProduct Manager: Simon Mackie Cover Designer: Alex WalkerTechnical Editor: Lionel Barrow 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 iv
📄 Page 6
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. v
📄 Page 7
(This page has no text content)
📄 Page 8
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.
📄 Page 9
(This page has no text content)
📄 Page 10
Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Conventions Used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi Tips, Notes, and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Supplementary Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Some Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii HTTP Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Go Get . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Guiding Gently, Not Explicitly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Want to take your learning further? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Chapter 1 Welcome New Gopher . . . . . . . . . . . . . . . . . . 1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The Go Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Your First Go Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Go Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Arrays and Slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
📄 Page 11
Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Type Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Exported and Unexported Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Chapter 2 Go Types Explored . . . . . . . . . . . . . . . . . . . . . . 23 Custom Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Embedded Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 The defer Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Third-party Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Syntax Options and Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Alternative Syntax Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Coding Style Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Chapter 3 HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Responding to Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Breaking It Down . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Adding More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Pattern Matching in Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Returning Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 The Handler Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Chaining Handlers to Create Middleware . . . . . . . . . . . . . . . . . . . . . 49 HTML Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Accessing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Conditionals with if and else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Loops with Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 x
📄 Page 12
Multiple Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Rendering JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Marshaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Marshaling Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Custom JSON Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Nested Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Unmarshaling Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Unknown JSON Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Chapter 4 Gophr Part 1: The Project . . . . . . . . . . . . . 69 Using Bootstrap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Project Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Serving Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Rendering Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Creating a Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Advanced Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Using httprouter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Flexible Middleware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 How Routing Works with Our Middleware . . . . . . . . . . . . . . . . . . . . 87 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Chapter 5 Gophr Part 2: All About the Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Before We Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 What do users look like? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Hashing a Password . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 xi
📄 Page 13
Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 User Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Creating Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Registration Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Registration Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Creating Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Persisting Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Chapter 6 Gophr Part 3: Remembering Our Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 What makes up a session? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Persisting User Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Checking for a User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Displaying User Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Signing Out, Signing In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 The Sign Out Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 The Login Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Editing Your Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Chapter 7 Gophr Part 4: Images . . . . . . . . . . . . . . . . . . 141 The Image Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 ImageStore Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Getting My(SQL) Groove On . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 MySQL Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Connecting to Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 xii
📄 Page 14
Creating ImageStore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Implementing ImageStore in DBImageStore . . . . . . . . . . . . . . 149 Uploading Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Chapter 8 Gophr Part 5: Concurrency . . . . . . . . . . 175 Goroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Waiting for Goroutines to Finish . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Communicating with Goroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Communicating with Multiple Channels . . . . . . . . . . . . . . . . . . . . . . . . . 182 Using Channels and Selects for Timeouts . . . . . . . . . . . . . . . . . . . . 183 Looping on selects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Throwing Away Goroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Putting Goroutines into Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Using the Resized Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Chapter 9 Automated Testing . . . . . . . . . . . . . . . . . . . . . 195 Writing Tests in Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Passing and Failing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Testing Multiple Variations of Inputs . . . . . . . . . . . . . . . . . . . . . . . . 198 Code Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Testing Between Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Testing HTTP Requests and Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Testing the Gophr Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Testing Remote HTTP requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Performance Benchmarking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Benchmark Regressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 xiii
📄 Page 15
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Chapter 10 Packaging and Production . . . . . . . . . . . 217 Creating Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 What is a package? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 An Example Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Exporting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Avoiding Circular Imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Let’s Implement a Real Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Package Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Dependency Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Godep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 The gopkg.in Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Deploying Go Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Compiling on the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Compiling Elsewhere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Cross-compiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Building Go for Other Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Building Binaries for Other Platforms . . . . . . . . . . . . . . . . . . . . . . . 234 That’s All, Folks! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 xiv
📄 Page 16
Preface The Internet is a place of constant evolution and creation. Nearly every day, web developers have new tools available to add to their repertoire. In recent years, though, few have been as influential as the Go programming language. Originally created at Google to solve system administration problems, Go has evolved into a modern, powerful, and well-adopted language. To call Go just a language would be a disservice, however; Go is more than a language—it’s an entire ecosystem. From the tools that come with it, to the community of developers that build on it, Go is a force majeure in the web development world and it is here to stay. So what is Go? Well, it’s a combination of a strongly typed programming language, and a collection of tools that make working with the language a pleasure. While many may consider a language to comprise merely the syntax, the tools provided to aid development are just as important—if not more so—as the language itself. I hope that as you work through this book you’ll see this for yourself, and learn to love Go as I do. The topics covered in this book are targeted at web development. While Go wasn’t created as a “language for web development,” it was produced with such a powerful and diverse standard library that web developers have taken to the language in droves, with many organizations now having Go applications powering their services. In this book, we won’t be covering every aspect of the standard library; instead we'll be diving deeply into how to create fast, powerful, and maintainable web applica- tions. Who Should Read This Book This book assumes at least a basic understanding of many programming and web development principles. If you’re unfamiliar with programming, or lack an under- standing of how to program for the Web, you may find the concepts discussed in this book hard to grasp. If, however, you’re a seasoned web developer, I hope that by seeing Go in action you’ll be inspired to try building “the next big thing” in Go.
📄 Page 17
Conventions Used You’ll notice that we’ve used certain typographic and layout styles throughout the book to signify different types of information. Look out for the following items: Code Samples Code in this book will be displayed using a fixed-width font, like so: <h1>A Perfect Summer's Day</h1> <p>It was a lovely day for a walk in the park. The birds were singing and the kids were all back at school.</p> If the code is to be found in the book’s code archive, the name of the file will appear at the top of the program listing, like this: example.css .footer { background-color: #CCC; border-top: 1px solid #333; } If only part of the file is displayed, this is indicated by the word excerpt: example.css (excerpt) border-top: 1px solid #333; If additional code is to be inserted into an existing example, the new code will be displayed in bold: function animate() { new_variable = "Hello"; } Where existing code is required for context, rather than repeat all the code, a vertical ellipsis will be displayed: xvi
📄 Page 18
function animate() { ⋮ return new_variable; } Some lines of code are intended to be entered on one line, but we’ve had to wrap them because of page constraints. A ➥ indicates a line break that exists for formatting purposes only, and should be ignored: URL.open("http://www.sitepoint.com/blogs/2015/05/28/user-style-she ➥ets-come-of-age/"); Tips, Notes, and Warnings Hey, You! Tips will give you helpful little pointers. Ahem, Excuse Me … Notes are useful asides that are related—but not critical—to the topic at hand. Think of them as extra tidbits of information. Make Sure You Always … … pay attention to these important points. Watch Out! Warnings will highlight any gotchas that are likely to trip you up along the way. Supplementary Materials http://www.learnable.com/books/go1/ The book’s website, which contains links, updates, resources, and more. https://github.com/spbooks/go1/ The downloadable code archive for this book. xvii
📄 Page 19
http://community.sitepoint.com/ SitePoint’s forums, for help on any tricky web problems. books@sitepoint.com Our email address, should you need to contact us for support, to report a prob- lem, or for any other reason. Some Notes HTTP Requests In many of the examples in this book, the intersting part of what's happening is less about what a browser is displaying and more about the way the browser interacts with a web server. We’re interested in seeing what data was sent in the request, such as the path we requested, and what headers we sent. In the response, we’re interested in what data comes back, not just in the response body, but also the headers that come back. In most of these cases, instead of showing a screenshot of a browser, I’ll simply show the raw HTTP request and response. It’s definitely not as pretty, but it’s a concise and clear way to get the data that I’m discussing onto the page. Don’t worry, there will still be plenty of screenshots. As an example, here’s what a request and response to the SitePoint website looks like: GET / HTTP/1.1 HTTP/1.0 200 OK Date: Mon, 01 Jun 2015 09:37:11 GMT Server: Apache/2.2.22 (Debian) Last-Modified: Mon, 01 Jun 2015 09:36:07 GMT Expires: Mon, 01 Jun 2015 10:36:07 GMT Content-Type: text/html; charset=UTF-8 Connection: close <!-- Page Content Omitted --> Generally I’ll avoid leaving any headers in the response that have no significance to what we’re talking about. xviii
📄 Page 20
You can generate your own requests from the command line by using the curl tool (you can download curl1 if you don’t already have it). For example, requesting SitePoint with curl would look like this: curl -i www.sitepoint.com. This is a great way to inspect what’s really going on under the hood. Go Get The go get tool is a very convenient package manager, but it’s also limited in func- tionality compared to a lot of package managers. When it fetches a package, it has no concept of version, in other words, it will get the latest version of the code you’re asking for. While this isn’t much of an issue when you’re working on your local machine. It means there’s a chance that when you go to run your code on another machine, such as during deployment, or if you’re part of a team, the new machine might download a different version of the package. Because of this, code that builds on your machine may not build on another machine because you’re running different versions of the packages. There are a few ways to get around this, and we’ll cover the options in a later chapter, but for the moment, be aware that if you are getting build errors that appear to ori- ginate from another package, it might be because the version you’ve downloaded is slightly different to the version I used when writing the code for this book. Whenever I introduce a new third party package, I will also note what version of that code I’m using, so if you’re comfortable with Git you can navigate to the library and check out the exact same version. Formatting There is a standard for how you should format your Go code, and although the compiler won’t throw an error if you format your code differently, it’s nice to follow the standards. You can completely automate this process by using the tool gofmt that comes with the Go installation. This tool will automatically reformat your Go code for you. All popular text editors will have a plugin that automatically runs gofmt for you when you save your files. I highly recommend taking the time to install one for your editor ― trust me, it’ll be one of the best things you ever do. 1 http://curl.haxx.se/dlwiz/?type=bin xix
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List