Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Go Web Development Cookbook
Go Web Development Cookbook

Go Web Development Cookbook: Build full-stack web applications with Go

eBook
$27.98 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Go Web Development Cookbook

Working with Templates, Static Files, and HTML Forms

In this chapter, we will cover the following recipes:

  • Creating your first template
  • Serving static files over HTTP
  • Serving static files over HTTP using Gorilla Mux
  • Creating your first HTML form
  • Reading your first HTML form
  • Validating your first HTML form
  • Uploading your first file

Introduction

Quite often, we would like to create HTML forms to get the information from a client in a specified format, upload files or folders to the server, and generate generic HTML templates, rather than repeating the same static text. With the knowledge of the concepts covered in this chapter, we will be able to implement all these functionalities efficiently in Go.

In this chapter, we will start with creating a basic template and then move on to serve static files, such as .js, .css, and images from a filesystem, and eventually create, read, and validate HTML forms and upload a file to the server.

Creating your first template

Templates allow us to define placeholders for dynamic content that can be replaced with the values at runtime by a template engine. They can then be transformed into an HTML file and sent to the client. Creating templates in Go is fairly easy using Go's html/template package, which we will be covering in this recipe.

How to do it...

In this recipe, we are going to create a first-template.html with a couple of placeholders whose value will be injected by the template engine at runtime. Perform the following steps:

  1. Create first-template.html inside the templates directory by executing the following Unix command:
$ mkdir templates && cd templates && touch first-template.html...

Serving static files over HTTP

While designing web applications, it’s always a best practice to serve static resources, such as .js, .css, and images from the filesystem, or any content delivery network (CDN), such as Akamai or Amazon CloudFront, rather than serving it from the web server. This is because all these types of files are static and do not need to be processed; so why should we put extra load on the server? Moreover, it helps to boost application performance, as all the requests for the static files will be served from external sources and therefore reduce the load on the server.

Go's net/http package is sufficient enough for serving static resources from the filesystem through FileServer, which we will be covering in this recipe.

Getting ready...

...

Serving static files over HTTP using Gorilla Mux

In the previous recipe, we served static resources through Go's HTTP file server. In this recipe, we will look at how we can serve it through the Gorilla Mux router, which is also one of the most common ways of creating an HTTP router.

Getting ready...

As we have already created a template which serves main.css from the static/css directory present on the filesystem in our previous recipe, we will just update it to use the Gorilla Mux router.

How to do it...

  1. Install the github.com/gorilla/mux package using the go get...

Creating your first HTML form

Whenever we want to collect the data from the client and send it to the server for processing, implementing an HTML form is the best choice. We will be covering this in this recipe.

How to do it...

In this recipe, we will create a simple HTML form that has two input fields and a button to submit the form. Perform the following steps:

  1. Create login-form.html inside the templates directory, as follows:
$ mkdir templates && cd templates && touch login-form.html
  1. Copy the following content to login-form.html:
<html>
<head>
<title>First Form</title>
</head>
<body>
<h1>Login</h1>
<form method="post" action=&quot...

Reading your first HTML form

Once an HTML form is submitted, we have to read the client data on the server side to take an appropriate action. We will be covering this in this recipe.

Getting ready...

Since we have already created an HTML form in our previous recipe, we will just extend the recipe to read its field values.

How to do it...

  1. Install the github.com/gorilla/schema package using the go get command, as follows:
$ go get github.com/gorilla/schema
  1. Create html-form-read.go, where we will read an HTML form field after decoding it using the github.com/gorilla/schema...

Validating your first HTML form

Most of the time, we have to validate a client's input before processing it, which can be achieved through the number of external packages in Go, such as gopkg.in/go-playground/validator.v9, gopkg.in/validator.v2, and github.com/asaskevich/govalidator.

In this recipe, we will be working with the most famous and commonly used validator, github.com/asaskevich/govalidator, to validate our HTML form.

Getting ready...

As we have already created and read an HTML form in our previous recipe, we will just extend it to validate its field values.

How to do it...

...

Uploading your first file

One of the most common scenarios in any web application is uploading a file or a folder to the server. For example, if we are developing a job portal, then we may have to provide an option where the applicant can upload their profile/resume, or, let's say, we have to develop an e-commerce website with a feature where the customer can upload their orders in bulk using a file.

Achieving the functionality to upload a file in Go is quite easy using its built-in packages, which we will be covering in this recipe.

How to do it...

In this recipe, we are going to create an HTML form with a field of type file, which lets the user pick one or more files to upload to a server via a form submission. Perform...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • •Become proficient in RESTful web services
  • •Build scalable, high-performant web applications in Go
  • •Get acquainted with Go frameworks for web development

Description

Go is an open source programming language that is designed to scale and support concurrency at the language level. This gives you the liberty to write large concurrent web applications with ease. From creating web application to deploying them on Amazon Cloud Services, this book will be your one-stop guide to learn web development in Go. The Go Web Development Cookbook teaches you how to create REST services, write microservices, and deploy Go Docker containers. Whether you are new to programming or a professional developer, this book will help get you up to speed with web development in Go. We will focus on writing modular code in Go; in-depth informative examples build the base, one step at a time. You will learn how to create a server, work with static files, SQL, NoSQL databases, and Beego. You will also learn how to create and secure REST services, and create and deploy Go web application and Go Docker containers on Amazon Cloud Services. By the end of the book, you will be able to apply the skills you've gained in Go to create and explore web applications in any domain.

Who is this book for?

This book is for Go developers interested in learning how to use Go to build powerful web applications. A background in web development is expected.

What you will learn

  • Create a simple HTTP and TCP web server and understand how it works
  • Explore record in a MySQL and MongoDB database
  • Write and consume RESTful web service in Go
  • Invent microservices in Go using Micro – a microservice toolkit
  • Create and Deploy the Beego application with Nginx
  • Deploy Go web application and Docker containers on an AWS EC2 instance

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Apr 23, 2018
Length: 338 pages
Edition : 1st
Language : English
ISBN-13 : 9781787288560
Vendor :
Google
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Apr 23, 2018
Length: 338 pages
Edition : 1st
Language : English
ISBN-13 : 9781787288560
Vendor :
Google
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 147.97
Go Web Development Cookbook
$48.99
Distributed Computing with Go
$43.99
Go Standard Library Cookbook
$54.99
Total $ 147.97 Stars icon

Table of Contents

12 Chapters
Creating Your First Server in Go Chevron down icon Chevron up icon
Working with Templates, Static Files, and HTML Forms Chevron down icon Chevron up icon
Working with Sessions, Error Handling, and Caching in Go Chevron down icon Chevron up icon
Writing and Consuming RESTful Web Services in Go Chevron down icon Chevron up icon
Working with SQL and NoSQL Databases Chevron down icon Chevron up icon
Writing Microservices in Go Using Micro – a Microservice Toolkit Chevron down icon Chevron up icon
Working with WebSocket in Go Chevron down icon Chevron up icon
Working with the Go Web Application Framework – Beego Chevron down icon Chevron up icon
Working with Go and Docker Chevron down icon Chevron up icon
Securing a Go Web Application Chevron down icon Chevron up icon
Deploying a Go Web App and Docker Containers to AWS Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
(2 Ratings)
5 star 50%
4 star 0%
3 star 0%
2 star 0%
1 star 50%
TeeBee Dec 06, 2019
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
This is a very poor effort. If you already know (even beginner) Go you don't need this book. If you don't know any Go this is not the place to start. Everything in it is easily available online and is usually more clearly presented - here, coverage is very shallow with no rigour and virtually no explanation. Many important topics are not covered at all. Something much better could be put together in a couple of weeks. To be avoided.
Amazon Verified review Amazon
Shashank Jun 05, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Go Web Development Cookbook is a fantastic resource for enterprise Go developers. Though easy to follow steps it takes you on a journey to deploy Go web application and Docker containers on Amazon Cloud Services.I specially like the chapter about Securing a Go Web Application where Arpit explained with the code how to move HTTP server to HTTPS, secure REST endpoints using JWT and prevent Cross-Site Request Forgery in Go Web Application.If you are a Go developer looking for a deep dive into the Go world, picking this book is a non brainer.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.