
Build a Go module for web development with the standard library, include tests, semantic versioning, and a workspace workflow, covering JSON handling, uploads, downloads, and authenticated static file delivery.
An experienced software contractor with 25 years in the field introduces their background, noting university teaching since 1991 and a PhD across English literature, computer science, and education.
Learn how to ask for help in Go module development by searching online, comparing with lecture source code, and posting concise questions with code or a git link.
Visit go.dev to download the appropriate Go installer for your OS, then follow the OS-specific instructions. After installation, run go version to verify the installed version.
Install an integrated development environment for Go by downloading Visual Studio Code, installing the official Go extension, adding go template syntax support, and running go colon install to install tools.
Set up a go workspace with toolkit and app folders, initialize go modules, configure a go work file, and create a simple toolkit type to start development.
Create a Go module that generates a 30-character random string for file names, write tests for each change, and build a simple app to push the module to GitHub.
Create an exported Go function to generate a random string of a given length from a predefined character source, including lowercase and uppercase letters, numbers, underscore, and plus sign.
Create a simple go app that uses a toolkit module, add idiomatic comments, generate a random string, run the program with go run, and prepare tests for the toolkit module.
write a unit test for the random string function in the Go toolkit. create tools_test.go, implement TestRandomString, check length ten, run go test, and commit to a git repository.
Install Git, create a GitHub repository, and push your Go module toolkit code by initializing Git, adding files, committing, and pushing.
Learn how to implement file uploads in a Go module. Enforce size limits and mime type checks by inspecting the first 512 bytes and testing with a web app.
Learn to upload one or more files with a Go toolkit's UploadFiles method, returning details like new and original names and size, with optional renaming and file type checks.
Create an app upload folder, initialize a Go module, and add it to the workspace to test file uploads in a simple Go project.
Build a simple Go web app to test file uploads, with routes to serve an html form and upload multiple or single files to an uploads folder.
Add a convenience method in the Go toolkit to upload a single file and return the uploaded file object, while ensuring the original file name is populated in the result.
Implement and run table-driven tests for the Go module's upload files function, simulating multipart uploads with pipes and a multipart writer, and verify allowed mime types and rename options.
Explore writing Go tests for uploading files, extending table-driven tests to cover rename permissions and disallowed file types, and validating the upload one file workflow.
Push your changes to GitHub after adding a readme and the MIT license to document the project and enable Go dev site documentation; commit and push, then verify formatting.
Add a Go module function to create a directory if it does not exist for file uploads, with a test and a simple app to try it.
Develop a Go method, CreateDirIfNotExists, that creates a directory at a given path with mode 0755 if it does not exist, returning any error.
Create and test a new create dir if not exist function in a go workspace, initializing modules, wiring toolkit imports, and running the program to generate a test directory.
Write a Go test for the create dir if not exists function, verify directory creation in test data, handle errors, and clean up after the test.
Update the UploadFiles method to leverage the new create directory if not exist logic, use equals operator for error handling, and run go test to confirm tests pass.
Push module updates to GitHub, commit changes including creating a directory with all parent directories, rename the title to tool kit, and prepare to add more functionality.
Add slug generation to the module to produce url-safe strings for web applications. The section covers implementing slug logic, testing it, and building a simple app to try it out.
Write a Go slugify method that converts a string to a URL-safe slug using regex, trims, and lowercases, replacing non alphanumerics with hyphens; error on empty results and add tests.
Add an app slug module to the go workspace, initialize go mod and go work, then run a main program that slugifies a string using a toolkit.
Develop and run table-driven tests for a Go slugify function, validating empty, simple, complex, non-roman, and roman-english inputs, and verify expected outputs and errors with go test.
Push updates to a git repository on GitHub, commit changes with a message about slug and tests, and manage branches and semantic versioning for releases.
Build a module function to securely download a static file from outside the document root to the user's browser, forcing a download for authenticated access, with a test app.
Implement the downloadStaticFile function in Go to force downloads by setting Content-Disposition to attachment and streaming the file with http.ServeFile using path.Join for cross-platform paths.
Build a small Go web app that serves a static file download via the toolkit's download static file function, with a home page and a download link on localhost:8080.
Implement a Go test for downloading a static file by creating test data, using an http test recorder, and validating content length, content disposition, and read errors.
Push your code changes to GitHub by committing updates to the readme and syncing the repository. Review the reflected changes in your browser and prepare for the JSON-focused section ahead.
Extend the Go module with functions that read and write JSON and generate clear JSON error responses. Apply these tools to simplify posting JSON to a remote API.
Learn to read JSON payloads in Go, decode them into Go data structures, enforce size and unknown-field safety, and respond with a structured JSON response.
Enhance the ReadJSON error handling by mapping standard library errors to precise messages, covering syntax errors, unmarshal type errors, unknown fields, and large request bodies in Go.
Learn to write a table test for read JSON in Go, validating max JSON size, unknown fields, and decoding into a foo struct via an http post.
Expand the Go table test for readjson by adding multiple cases, including badly formatted json, incorrect types, two json files, empty body, syntax errors, unknown fields, and size limits.
Learn how to write JSON in Go by creating a writeJSON function that marshals data, sets content-type and status, and optional headers for an http response.
Write a unit test for the write JSON function in Go, using a response recorder, payload setup, and headers; verify no error and correct JSON output.
Create a reusable errorJSON function on the tools module that sends a JSON error response via an http response writer, with an optional status code defaulting to bad request.
Write a Go test for the error JSON function, validating a service unavailable response, decoding the JSON payload, and ensuring the error flag is true.
Learn to push json to a Go API by building a reusable function that marshals data, posts json, and returns the status code and error with an optional http client.
Learn to test push json to remote in Go (Golang) by mocking the http client with a custom round trip function, creating a test client, and simulating remote responses.
Set up a simple Go web app by creating an app JSON module, adding HTML and bootstrap styling, and using fetch to post JSON to a local server.
Build a basic Go web app by defining request and response payload types, configuring a mux router, serving html, handling post requests, and starting an 8081 server.
Build a Go handler that receives a JSON post, reads and validates it into a request payload, and returns a JSON response payload with proper error handling.
Learn to push JSON to a remote service in a Go module by creating a simulated remote API, wiring HTTP calls, and validating status codes for end-to-end testing.
Discover semantic versioning basics for go module development, identifying major, minor, and patch versions. Practice tagging release 1.0.0, then implement a breaking change to 2.0.0 with updated code and tests.
Master semantic versioning and how major, minor, and patch numbers indicate compatibility, with 1.0.0 and 2.0.0 examples; learn to tag versions and upgrade when features change or bugs are fixed.
Tag version 1.0.0 of our Go module by updating the readme, committing changes, and publishing a release tag, then plan a non backwards compatible change for version 2.0.0.
Tag version 2.0.0 of the module by creating a v2 directory, updating the go mod path to v2, and applying a breaking change that breaks backwards compatibility with adjusted tests.
Import the module into a standalone project, create a dummy API with two endpoints, and test importing from GitHub to ensure it works without Go workspaces.
Create a simple dummy api in Go to test your module when importing it from GitHub, setting up a web app with login and logout routes on a local server.
Import the toolkit module from GitHub with go get version two, then implement a logout endpoint returning a toolkit json response with a logged out message and http status accepted.
Implement a go login handler, serve a bootstrap-styled html form, post email and password as json, and return a structured json response indicating success or failure.
Working with web applications in Go is remarkably easy, but it does not take too long to realize that in a lot of cases, we end up writing the same kind of code every time we start a new project. You might need to read JSON, write JSON, upload files, or any of the commonly used features of a given web application. In other words, we often rewrite code that we have already written, many times over.
Rather than simply copying and pasting code from one project to another, it makes sense to take advantage of Go modules -- reusable code that can be included in a project by simply issuing a "go get" command. That way, if new functionality is added to that module, any project that imports it can take advantage of that functionality simply by updating its dependencies, and if a bug is discovered, you can fix it by updating the module; every project that uses that module gets the bug fix with a single "go get -u" command.
Building a robust, secure, well-tested module is not difficult, but it does require careful planning to ensure that it will work across different operating systems.
This course will take you through the steps necessary to produce a module that includes many of the tasks commonly used in web applications, including:
reading JSON
writing JSON
producing well formatted, informative error messages
uploading one or more files to a server, and limiting uploads by mime type and file size
creating directories on the server
generating random strings
downloading static files
posting JSON to a remote service
creating a URL safe slug from a string
We'll build our module using Go 1.18's new workspaces tools, and we'll ensure that the entire module is well-tested.
Our final module will not have any external dependencies, and will only use functionality found in Go's standard library.
By the time you have finished this course, you'll have a Go module that will make starting a new web application much faster, and you won't be depending on someone else's code to do so.