Makefile is my go-to tool for every project now. Instead of running long commands inside the repo, I use Makefile. Makefile is super awesome!
For my blog, while building, there were lots of commands I needed to run, like building the binary, running the binary, building the docker image, and re-building the docker image every time I made changes in my code. I had to run many commands.
go build -o blog ./cmd/api/main.go
sudo docker compose up -d
sudo docker compose up --build -d
Sometimes I became so lazy that I pressed the up arrow key lots of times because there were many other commands I had executed.
That’s why I thought I should use Makefile. You just set up the Makefile and run the command make build
. Once you set it up, you can see the generated binary. You can easily remember what to run; you don’t have to type the full go build -o blog ./cmd/api/main.go
or other project-related commands all the time. It’s just easy and cool.
Example Makefile:
BINARY_NAME = blog
CLI_BINARY_NAME = blog_cli
API_ENTRY = cmd/api/main.go
CLI_ENTRY = cmd/cli/main.go
# Build the main API binary.
build:
@echo "==> Building API binary: $(BINARY_NAME)"
@go build -ldflags="-s -w" -o $(BINARY_NAME) $(API_ENTRY)
# Build the command-line interface (CLI) binary.
build-cli:
@echo "==> Building CLI binary: $(CLI_BINARY_NAME)"
@go build -ldflags="-s -w" -o $(CLI_BINARY_NAME) $(CLI_ENTRY)
# Run the API application directly.
run:
@echo "==> Running API from $(API_ENTRY)..."
@go run $(API_ENTRY)
# Build and run the docker containers.
build-docker: docker-setup
@echo "==> Starting Docker containers..."
@sudo $(COMPOSE_CMD) up -d
# Rebuild and run the docker containers.
re-build-docker: docker-setup
@echo "==> Rebuilding and starting Docker containers..."
@sudo $(COMPOSE_CMD) up --build -d
# Shut down and remove the docker containers.
docker-down:
@echo "==> Shutting down Docker containers..."
@sudo $(COMPOSE_CMD) down
Now you just have to run make build
or any other command you like. If you don’t know what command to run, simply run make help
, it will list all the available commands in the Makefile.
Once I was working on a project where I used Air for live reloading, TailwindCSS for generating CSS, and Templ for generating user interfaces.
While building, I had to run these three commands:
tailwindcss -i ./styles/input.css -o ./assets/css/output.css --watch --minify
templ generate -path ./cmd/web/templates --watch --open-browser=false
air
You can easily remember air
, but the other two commands are a bit long, and I had to press the up arrow key countless times.
Again, Makefile came to the rescue.
Example:
TEMPLATES_DIR = cmd/web/templates
TAILWIND_INPUT = cmd/web/styles/input.css
TAILWIND_OUTPUT = cmd/web/assets/css/output.css
watch-air:
@echo "==> Starting Air for live reload..."
@air
watch-templ:
@echo "==> Watching templ files..."
@templ generate -path $(TEMPLATES_DIR) --watch --open-browser=false
watch-css:
@echo "==> Watching CSS files..."
@tailwindcss -i $(TAILWIND_INPUT) -o $(TAILWIND_OUTPUT) --minify --watch
However, if you try to run the commands like this:
watch:
@$(MAKE) watch-templ
@$(MAKE) watch-air
@$(MAKE) watch-css
This method won’t work because here we are executing three commands sequentially, so the second command waits for the first to finish.
Now we have to run these commands in parallel:
watch:
@echo "==> Starting live reload with Air, Templ, and Tailwind CSS..."
@$(MAKE) -j3 watch-air watch-templ watch-css
The make -jN
option specifies the maximum number of commands that make
can run in parallel.
Read more about $(MAKE)
here: https://stackoverflow.com/questions/38978627/what-is-the-variable-make-in-a-makefile
Just run make watch
and you’re good to go. Makefile just makes your development process much easier and more efficient. I hope you use Makefile in your project!
Top comments (0)