Book Image

Full-Stack Web Development with Go

By : Nanik Tolaram, Nick Glynn
Book Image

Full-Stack Web Development with Go

By: Nanik Tolaram, Nick Glynn

Overview of this book

Go is a modern programming language with capabilities to enable high-performance app development. With its growing web framework ecosystem, Go is a preferred choice for building complete web apps. This practical guide will enable you to take your Go skills to the next level building full stack apps. This book walks you through creating and developing a complete modern web service from auth, middleware, server-side rendering, databases, and modern frontend frameworks and Go-powered APIs. You’ll start by structuring the app and important aspects such as networking, before integrating all the different parts together to build a complete web product. Next, you’ll learn how to build and ship a complete product by starting with the fundamental building blocks of creating a Go backend. You’ll apply best practices for cookies, APIs, and security, and level up your skills with the fastest growing frontend framework, Vue. Once your full stack application is ready, you’ll understand how to push the app to production and be prepared to serve customers and share it with the world. By the end of this book, you’ll have learned how to build and ship secure, scalable, and complete products and how to combine Golang with existing products using best practices.
Table of Contents (21 chapters)
1
Part 1: Building a Golang Backend
5
Part 2:Serving Web Content
9
Part 3:Single-Page Apps with Vue and Go
14
Part 4:Release and Deployment

Installing sqlc

We have defined the database structure so now it’s time to talk a bit more about the tool that we are going to be using called sqlc. sqlc is an open source tool that generates type-safe code from SQL; this allows developers to focus on writing SQL and leave the Go code to sqlc. This reduces the development time, as sqlc takes care of the mundane coding of queries and types.

The tool is available at https://github.com/kyleconroy/sqlc. The tool helps developers focus on writing the SQL code that is needed for the application and it will generate all the relevant code needed for the application. This way, developers will be using pure Go code for database operations. The separation is clean and easily trackable.

The following diagram shows the flow that developers normally adopt when using the tool at a high level.

Figure 1.3 – Flow to use sqlc to generate Go code

Figure 1.3 – Flow to use sqlc to generate Go code

All SQL code will be written in .sql files, which will be read and converted by the sqlc tool into the different Go code.

Download and install SQL binary by using the following command:

go install github.com/kyleconroy/sqlc/cmd/sqlc@latest

Make sure your path includes the GOPATH/bin directory – for example, in our case, our path looks like the following:

…:/snap/bin:/home/nanik/goroot/go1.16.15/go/bin:/home/nanik/go/bin

If you don’t have GOPATH as part of the PATH environment variable, then you can use the following command to run sqlc:

$GOPATH/bin/sqlc
Usage:
  sqlc [command]
Available Commands:
  compile     Statically check SQL for syntax and type
  errors
  completion  Generate the autocompletion script for the
  specified shell
  generate    Generate Go code from SQL
  help        Help about any command
  init        Create an empty sqlc.yaml settings file
  upload      Upload the schema, queries, and configuration
  for this project
  version     Print the sqlc version number
Flags:
  -x, --experimental   enable experimental features (default: false)
  -f, --file string    specify an alternate config file (default: sqlc.yaml)
  -h, --help           help for sqlc

Use "sqlc [command] --help" for more information about a command.

At the time of writing, the latest version of sqlc is v1.13.0.

Now that we have installed the tool and understand the development workflow that we will be following when using the tool, we will look at how to use the tool for our application.