26,39 €
Build frontend-to-backend web applications using the best practices of a powerful, fast, and easy-to-deploy server language
This book is intended for experienced programmers with minimal-to-moderate exposure to the Go language. If you have some fundamentals down, but are looking for more detail when it comes to using Go for the web, this is the book for you.
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. It is a statically typed language with syntax loosely derived from that of C, adding garbage collection, type safety, some dynamic-typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library.
Learning Go Web Development is a start-to-finish walkthrough of the topics most critical to anyone building a new web application. Whether it's keeping your application secure, connecting to your database, enabling token-based authentication, or utilizing logic-less templates, this book has you covered. You'll begin by learning about routing requests and implementing SSL. Moving on, you'll get to know about practices to keep users' data safe. By the end of the book, you will be able to build robust, secure, and fully-featured applications for the web.
This concise book demonstrates by doing. You'll build a component every step of the way and in the end, will have a blogging platform that can be used for personal experimentation and augmented to be used in production.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 147
Veröffentlichungsjahr: 2016
Copyright © 2016 Packt Publishing
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 embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2016
Production reference: 1220416
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-231-7
www.packtpub.com
Author
Nathan Kozyra
Reviewer
Karthik Nayak
Commissioning Editor
Ashwin Nair
Acquisition Editor
Divya Poojari
Content Development Editor
Kajal Thapar
Technical Editor
Devesh Chugh
Copy Editor
Sneha Singh
Project Coordinator
Shweta H Birwatkar
Proofreader
Safis Editing
Indexer
Rekha Nair
Graphics
Abhinash Sahu
Production Coordinator
Manu Joseph
Cover Work
Manu Joseph
Nathan Kozyra is a seasoned web developer, with nearly two decades of professional software development experience. Since Go's initial release, he has been drawn to the language for its power, elegance, and usability.
He has a strong interest in web development, music production, and machine learning. He is married and has a two-year-old son.
Karthik Nayak is currently studying at BMSIT, Bangalore. He has been continuously contributing to Git ever since he took part in GSOC 2015. He has also been working on Linux kernel and taking part in the Eudyptula challenge. He learned Go to get familiar with the Web and to know how backends are generally designed.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
Thank you for purchasing this book. We hope that through the examples and projects in this book, you'll move from being a Go web development neophyte to someone who's able to take on serious projects intended for production. As such, this book tackles a lot of web development topics at a relatively high level. By the end of the book, you should be able to implement a very simple blog that accommodates display, authentication, and commenting with an eye towards performance and security.
Chapter 1,Introducing and Setting up Go, starts the book by showing you how to set up your environment and dependencies so that you can create web applications in Go.
Chapter 2, Serving and Routing, talks about producing responsive servers that react to certain web endpoints. We'll explore the virtues of various URL routing options beyond net/http.
Chapter 3, Connecting to Data, implements database connections to start acquiring data to be presented and manipulated using our website.
Chapter 4, Using Templates, covers the template packages to show how we can present the data that we're using and modifying to the end user.
Chapter 5, Frontend Integration with Restful APIs, takes a detailed look at how to create an underlying API to drive both the presentation and the functionality.
Chapter 6, Sessions and Cookies, maintains state with our end users, thus allowing them to retain information, such as authentication, from page to page.
Chapter 7, Microservices and Communication, tears apart some of our functionality to be reimplemented as microservices. This chapter will serve as a light introduction to the microservice ethos.
Chapter 8, Logging and Testing, talks about how a mature application will require both testing and extensive logging to debug and catch issues before they make it to production.
Chapter 9, Security, will focus on the best practices for web development in general and review what Go provides for the developer in this space.
Chapter 10, Caching, Proxies, and Improved Performance, reviews the best options for ensuring that there are no bottlenecks or other issues that could negatively impact performance.
Go excels at cross-platform compatibility, so any modern computers running a standard Linux flavor, OS X or Windows should be enough to get started. You can find a full list of requirements at https://golang.org/dl/. In this book, we are working with a minimum of Go 1.5, but any newer release should be fine.
This book is intended for developers who are new to Go but have previous experience of building web applications and APIs. If you are aware of HTTP protocols, RESTful architecture, general templating and HTML, you should be well prepared to take on the projects in this book.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/LearningGoWebDevelopment_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
When starting with Go, one of the most common things you'll hear being said is that it's a systems language.
Indeed, one of the earlier descriptions of Go, by the Go team itself, was that the language was built to be a modern systems language. It was constructed to combine the speed and power of languages, such as C with the syntactical elegance and thrift of modern interpreted languages, such as Python. You can see that goal realized when you look at just a few snippets of Go code.
From the Go FAQ on why Go was created:
"Go was born out of frustration with existing languages and environments for systems programming."
Perhaps the largest part of present-day Systems programming is designing backend servers. Obviously, the Web comprises a huge, but not exclusive, percentage of that world.
Go hasn't been considered a web language until recently. Unsurprisingly, it took a few years of developers dabbling, experimenting, and finally embracing the language to start taking it to new avenues.
While Go is web-ready out of the box, it lacks a lot of the critical frameworks and tools people so often take for granted with web development now. As the community around Go grew, the scaffolding began to manifest in a lot of new and exciting ways. Combined with existing ancillary tools, Go is now a wholly viable option for end-to-end web development. But back to that primary question: Why Go? To be fair, it's not right for every web project, but any application that can benefit from high-performance, secure web-serving out of the box with the added benefits of a beautiful concurrency model would make for a good candidate.
In this book, we're going to explore those aspects and others to outline what can make Go the right language for your web architecture and applications.
We're not going to deal with a lot of the low-level aspects of the Go language. For example, we assume you're familiar with variable and constant declaration. We assume you understand control structures.
In this chapter we will cover the following topics:
The most critical first step is, of course, making sure that Go is available and ready to start our first web server.
While one of Go's biggest selling points is its cross-platform support (both building and using locally while targeting other operating systems), your life will be much easier on a Nix compatible platform.
If you're on Windows, don't fear. Natively, you may run into incompatible packages, firewall issues when running using go run command and some other quirks, but 95% of the Go ecosystem will be available to you. You can also, very easily, run a virtual machine, and in fact that is a great way to simulate a potential production environment.
In-depth installation instructions are available at https://golang.org/doc/install, but we'll talk about a few quirky points here before moving on.
For OS X and Windows, Go is provided as a part of a binary installation package. For any Linux platform with a package manager, things can be pretty easy.
To install via common Linux package managers:
Ubuntu: sudo apt-get golang
CentOS: sudo yum install golang
On both OS X and Linux, you'll need to add a couple of lines to your path—the GOPATH and PATH. First, you'll want to find the location of your Go binary's installation. This varies from distribution to distribution. Once you've found that, you can configure the PATH and GOPATH, as follows:
While the path to be used is not defined rigidly, some convention has coalesced around starting at a subdirectory directly under your user's home directory, such as $HOME/go or ~Home/go. As long as this location is set perpetually and doesn't change, you won't run into issues with conflicts or missing packages.
You can test the impact of these changes by running the go env command. If you see any issues with this, it means that your directories are not correct.
Note that this may not prevent Go from running—depending on whether the GOBIN directory is properly set—but will prevent you from installing packages globally across your system.
To test the installation, you can grab any Go package by a
