- Routing
- Validation
- Migration
- Models
- Rate Limiter
- Logger
- Graceful Shutdown
- Background Tasks
- Tokens
- Permissions
- Cors
- Metrics (expvar, httpsnoop)
- Makefile
- The
bindirectory will contain our compiled application binaries, ready for deployment to a production server. - The
cmd/apidirectory 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
internaldirectory 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/apiwill import the packages in theinternaldirectory (but never the other way around). - The
migrationsdirectory 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.modfile will declare our project dependencies, versions and module path. - The
Makefilewill contain recipes for automating common administrative tasks — like auditing our Go code, building binaries, and executing database migrations.
Download dependency
go mod downloadInstall the package
go install -v ./...Install migration tools (macOS)
brew install golang-migrateCreating migration
make migration/new name=create_example_tableUp
make migration/upRun
make run/apiBuild
make build/apiPrint makefile help messages
make helpgo fmt ./...command to format all .go files in the project directory, according to the Go standard.go vet ./...command to check all.gofiles 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
staticchecktool to carry out some additional static analysis checks.
make auditgo mod tidycommand will make sure the go.mod and go.sum files list all the necessary dependencies for our project (and no unnecessary ones).go mod verifycommand 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 vendorcommand will then copy the necessary source code from your module cache into a new vendor directory in your project root.
make vendor