Creates a starter project folder for development of Go backends using a single command.
The following has to be installed prior to running the script:
- Git
- Go
- Bash
Clone the repo:
git clone git@github.com:David-The-Programmer/create-go-backend.git
Run the shell script create a new Go project folder:
./create.sh
You would be prompted for:
-
The path of the new project folder
Take note that you can use either:
- A relative path (to your current directory). For example,
./project
or../project
. - A absolute path. For example,
~/project
.
- A relative path (to your current directory). For example,
-
The go module path
Please read the go documentation on specifying the go module path.
-
The go version
Only the version number needs to be entered, for e.g,
1.17
or1.21.7
.All go release versions can be found here.
Once the project is generated, the Dockerfile
and compose.yml
files can be deleted if usage of docker is not required.
Run the following command to start running the Go backend:
docker compose up --watch
If errors popup while building the Docker image, run the following command after fixing the error to force docker compose
to rebuild the Docker image and ignore the cache.
docker compose build
Subsequently run docker compose up --watch
to start the container.
The interesting thing is that --watch
flag will cause docker compose
to watch any changes made to the Go code and rebuild upon change.
This solves the issue of constantly manually rebuilding the code, which is a pain :(
The .vscode
folder and the .devcontainer.json
file can be deleted if VSCode would not be used as the editor of choice.
If the Go version has to be changed after the project folder has been created, take note to change:
- The
.env
file. This file contains theGO_VERSION
environment variable, which is used bydocker compose
to know which Go version to use when building the docker image. Only changing the value of theGO_VERSION
variable is required, there is no need to export the variable manually,docker compose
automatically interpolates the variable value from the.env
file. - The
go.mod
file. Rungo mod edit -go=<new go version>
. This will correct the Go version in thego.mod
file.
Subsequently, run docker compose build
to force rebuilding of the docker image before running docker compose up --watch
.
This is because docker compose up --watch
uses the cached image by default, hence the change in Go version would not be reflected.
- Initialise Git in project folder?
- Rewrite in Go?
- Automated tests to ensure script creates project folder properly (including
docker compose up --watch
?) - Improving folder name and module path prompts to ensure user does not give invalid inputs (e.g, no input, input with only spaces, module name not matching folder name, etc)
- Warn user of directory override when project folder path already exists
- Re-prompt user if user does not want to override existing folder in project folder path
- Ensure script stops when there are errors
- Display any actions completed/errors during execution of the script
- Inclusion of git hooks for testing and linting
- Compatibility of
Dockerfile
andcompose.yml
with.devcontainer.json
(maybe a separate way to make sure vscode uses workspace specific configs, lang version and extensions?)