A simple go project structure setup with all dependencies you need to get your MVP off the ground. 🚀
The project is suited as starting point for a simple webapp MVP. Besides the core files needed to get the setup running it contains a simple user module showcasing the intended project structure. Running the project starts the app exposing a REST API using the go-shipped webserver.
Features
- Hot reloading.
- sqlite3 database for local development. ❤️
- ORM ready to go. 🏃
- A simple database migration system. 🙌
- Injection of environment variables into config files. 🎶
- Simple spring-like repository structure for convenient database queries. ⭐
- Testing setup. 👷
Feedback and PRs welcome! I hope you find it useful. 🍺 🍕
$ git clone https://github.com/eldonaldo/go-project-template
$ cd go-project-template
# You need to get air separately to get the hot reloading running
$ go get -u github.com/cosmtrek/air
# Runs the project with hot reloading enabled
$ air
Interact with the app using the command line 💻
$ curl localhost:8008/create?name=John
$ User with name John1 created
$ curl localhost:8008/greet?name=John
$ Hi John
$ {
$ "ID": 1,
$ "CreatedAt": "2021-04-07T21:32:14.135761+02:00",
$ "UpdatedAt": "2021-04-07T21:32:14.135761+02:00",
$ "DeletedAt": null,
$ "Name": "John"
$ }
- cmd: Contains the app binaries
- core: The app's core functionality resides here (such as database handling, migrations etc.)
- core/db/migrations: SQL migration files go in here. Use
./scripts/migration_create.sh migration_name
to create a new one. Migrations in this folder are automatically executed (in order) once upon app start. - scripts: Automation scripts. There is a script to bootstrap a new migration and another to downgrade an already applied migration.
- server: Exposes the app as REST API and handles the HTTP server setup.
- user: A user module showcasing a simple structure using repository and REST handler.
To change the project name from go-project-name to something else you need to edit the go.mod module end edit the
first line module github.com/eldonaldo/go-project-template
to module github.com/your-username/your-new-name
. Imports
in all files need to be changed according (your IDE probably can do that for you ♻️). Further, you might also
want change cmd/project-name
and therefore you also need to change line 6 of .air.conf
. But that should be it
then. ✅
- github.com/caarlos0/env - Read env variables into struct fields
- github.com/pressly/goose - Database migrations
- gorm.io/gorm - ORM
- github.com/cosmtrek/air - Hot reloading
- github.com/stretchr/testify/assert - For convenient testing
👋