Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Full-Stack Web Development with Go

You're reading from   Full-Stack Web Development with Go Build your web applications quickly using the Go programming language and Vue.js

Arrow left icon
Product type Paperback
Published in Feb 2023
Publisher Packt
ISBN-13 9781803234199
Length 302 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Nick Glynn Nick Glynn
Author Profile Icon Nick Glynn
Nick Glynn
Nanik Tolaram Nanik Tolaram
Author Profile Icon Nanik Tolaram
Nanik Tolaram
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Part 1: Building a Golang Backend
2. Chapter 1: Building the Database and Model FREE CHAPTER 3. Chapter 2: Application Logging 4. Chapter 3: Application Metrics and Tracing 5. Part 2:Serving Web Content
6. Chapter 4: Serving and Embedding HTML Content 7. Chapter 5: Securing the Backend and Middleware 8. Chapter 6: Moving to API-First 9. Part 3:Single-Page Apps with Vue and Go
10. Chapter 7: Frontend Frameworks 11. Chapter 8: Frontend Libraries 12. Chapter 9: Tailwind, Middleware, and CORS 13. Chapter 10: Session Management 14. Part 4:Release and Deployment
15. Chapter 11: Feature Flags 16. Chapter 12: Building Continuous Integration 17. Chapter 13: Dockerizing an Application 18. Chapter 14: Cloud Deployment 19. Index 20. Other Books You May Enjoy

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.

You have been reading a chapter from
Full-Stack Web Development with Go
Published in: Feb 2023
Publisher: Packt
ISBN-13: 9781803234199
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime