Home
Sign in Buy now

Developer Experience

Our toolbox

Completed
Master Fullstack Golang Logo

Watch for free

Create a free account to watch this video.

Start for free

Summary

In this episode, we explore Go 1.24's tool directive feature, which allows adding development tools to go.mod with specific versions instead of installing them globally. We add four essential tools to the project: Templ for templating, Goose for database migrations, sqlc for generating type-safe Go code from SQL queries, and Air for hot reloading during development. This approach ensures all team members use the same tool versions and makes the development workflow more consistent across the team.

Transcript

So in this project, we are going to be using some tools and we're going to be utilizing what is known as the tool directive, which was added in Go 1.24, where we can simply use a flag whenever we do a go get to get a dependency into our go.mod file. That will add it as a tool instead of a, let's call it application dependency. This makes it a lot easier to have tools in our project that has a specific version and also that you can share across a team so you don't have one developer using one version of something and then another developer using another version, which is kind of what we had before where you had to do go install and that would install a tool globally on your machine instead of having a specific version. We've already talked a little bit about one of these tools, which is the one called Templ. But we need more, we need some code generation, we need some what is known as hot reloading, we need to generate migrations and apply migrations. So let me just quickly show you how we can add Templ to the project and not globally on our machine. And the way we do that is we say go get -tool. So I also have Templ installed globally on my machine on version 0.2.793. But if I check go.mod file, we can see I have a bunch of other tools as well that we will walk through in just a second, but we can see we have the Templ tool here. And if I just search, we can see we actually use 0.2.960, which was the one we just added. So doing this makes it easier to share tools for our project across different developers. And we can also create commands that makes it easier to work with these tools because now it's just a go run and then the name of the tool and then the command we want to run. That's for the next episode where we're going to be setting up something called justfile, which is a modern alternative to makefile if you know about that, but it's just a way that we can quickly create some commands that we're going to be running a lot. Great. This was how we can add tools and we only just added Templ. And some of the other benefits about this is that whenever we want to go mod tidy, it doesn't get removed. Everyone who accesses this repository will know exactly what dependencies and which version they have. Now we also need to do migrations and for that, we're going to be using a tool called Goose, which also lives on GitHub. So we just say go get -tool github.com/pressly/goose/v3/cmd/goose@latest. And now we can generate migration files, which we'll get to later on in the course. But now we can say go run and then Goose and it will take a minute to load the first time. And then now we can actually see it and it goes much faster the second time and we have access to this tool called Goose that we can work with migrations on our database. Next up, we need something called sqlc, which is what we're going to be using to take SQL files and then turn them into type safe Go code. I'm just going to say go get -tool github.com/sqlc-dev/sqlc/cmd/sqlc@latest. And then we need to jump into our database directory. And here I already have set up an sqlc.yaml file, which is how we configure this tool so it knows what engine we are going to be using. We're going to be using Postgres. We can also specify the gen go and then a package. So this is just some information about what to generate and where it should generate it. So we can see here we put the generated code in the internal models DB directory. And the reason we do this is that we want to keep all database access to come only through models. This means that the only way that you can access the database is through our models, which is where all of our business logic will live. We also specify we want a file called db.go and entities.go which is going to be technically the same as models. We are going to be touching upon what models are in depth later on in the course. So I'm just going to differentiate between what we call models and what sqlc defines as models, even though they are going to be close to each other. We're also going to emit SQL as a comment. So in our model whenever we import one of these generated functions, we can also quickly see what SQL is running. And then we're going to be using the pgx version 5 SQL package. And finally, we want to specify that for any UUID in the database, we want to use this package. And we also need to add a migrations folder and a queries folder. In here is where we're going to write all of our SQL queries. So for example, user.sql and then in here we can write plain old SQL that then gets compiled into type safe Go code. The final tool we need is something called Air and that's because right now or up to now we have been starting and stopping our Go server manually, which is fine at first but it can slow down development quite quickly. So we want to add something that can hot reload or live reload our changes. So whenever we make a change to our Go file or to the Templ files, we have something that recompiles the project so we can see our latest changes and for that we're going to be using this tool called Air. So go get -tool github.com/air-verse/air@latest and we just upgraded from what was in the go.mod file. In the next episode, we will set up our justfile and configure these tools with some commands so we can more easily run them without having to say go run and then the command every time. We need to specify where to build and what it should look for. With sqlc we need to specify the input and the output. With Goose we also need to specify the input path of where our migrations are and where should we output the migration. So that we're going to tackle in the next episode.

Episode Notes

Get the Full Course

Unlock all episodes and get lifetime access to course updates.

What others say

"With this course, I'm learning Golang + datastar while building my personal blog hitting several birds with one stone. It's a fantastic course."

Testimonial from ignacio
Ignacio Barceló
Bought Early Access

Video Completed!

Great job finishing this episode

Up Next

Developer Experience
Justfile