10 Sending and receiving data

 

This chapter covers

  • Serving static files like CSS, images, and JavaScript
  • Embedding static files directly in your compiled binary
  • Handling HTML forms, including dealing with file uploads
  • Working with raw multipart messages

In a way, the original web service was simply serving files. The kind of interactive web we enjoy today wasn’t there when the HTTP protocol was publicly released to the internet in 1991. When it came, it did so through web forms. These constructs, created decades ago, are still the foundation of the web and continue to power modern web applications.

This chapter starts by presenting methods to serve static files for your Go application. Because what you create in Go is a web server, rather than running behind a separate web server such as Nginx, you need to set up how you want files such as Cascading Style Sheets (CSS), JavaScript, images, or other files to be served. You’ll learn several ways to store and serve files that provide solutions for varying applications.

From there, we move to form handling, which we saw the basics of in Go in chapter 9, but for advanced cases such as handling files as multipart form data, more advanced techniques are required, especially if you want to work with large files.

10.1 Serving static content

10.1.1 Serving subdirectories

10.1.2 Using a file server with custom error pages

10.1.3 Embedding files in a binary

10.1.4 Serving from an alternative location

10.2 Advanced form processing

10.2.1 Handling form requests

10.2.2 Accessing multiple values for a form field

10.2.3 Working with files and multipart submissions

10.2.4 Uploading multiple files

10.2.5 Verifying that uploaded file is of an allowed type

10.2.6 Saving a file incrementally

Summary