- Routing
- Validation
- Migration
- Models
- Rate Limiter
- Logger
- Graceful Shutdown
- Background Tasks
- Tokens
- Permissions
- Cors
- Metrics (expvar, httpsnoop)
- Makefile
- The
bin
directory will contain our compiled application binaries, ready for deployment to a production server. - The
cmd/api
directory will contain the application-specific code for our Greenlight API application. This will include the code for running the server, reading and writing HTTP requests, and managing authentication. - The
internal
directory will contain various ancillary packages used by our API. It will contain the code for interacting with our database, doing data validation, sending emails and so on. Basically, any code which isn’t application-specific and can potentially be reused will live in here. Our Go code undercmd/api
will import the packages in theinternal
directory (but never the other way around). - The
migrations
directory will contain the SQL migration files for our database. The remote directory will contain the configuration files and setup scripts for our production server. - The
go.mod
file will declare our project dependencies, versions and module path. - The
Makefile
will contain recipes for automating common administrative tasks — like auditing our Go code, building binaries, and executing database migrations.
Download dependency
go mod download
Install the package
go install -v ./...
Install migration tools (macOS)
brew install golang-migrate
Creating migration
make migration/new name=create_example_table
Up
make migration/up
Run
make run/api
Build
make build/api
Print makefile help messages
make help
go fmt ./...
command to format all .go files in the project directory, according to the Go standard.go vet ./...
command to check all.go
files in the project directory. Runs a variety of analyzers which carry out static analysis of your code and warn you about things which might be wrong but won’t be picked up by the compiler.go test -race -vet=off ./...
command to run all tests in the project directory.- Third-party
staticcheck
tool to carry out some additional static analysis checks.
make audit
go mod tidy
command will make sure the go.mod and go.sum files list all the necessary dependencies for our project (and no unnecessary ones).go mod verify
command will verify that the dependencies stored in your module cache (located on your machine at $GOPATH/pkg/mod) match the cryptographic hashes in the go.sum file.go mod vendor
command will then copy the necessary source code from your module cache into a new vendor directory in your project root.
make vendor