Hi my name is Rashid Ali, I recently started learning about Golang and I have decided to document it so that it can be helpful for other people who are getting started as well.
Before you start, make sure you have the following installed:
git clone https://github.com/rashidkalwar/go-crud.git
cd go-crud
PORT=8080
DB_URL="postgres://<username>:<password>@<IpAddress>:<PORT>"
.Note: If you have PostgreSQL installed on your system you can use that OR use Docker to spin up a PostgreSQL database OR use a free online PostgreSQL database. I would suggest Supabase it has a generous free tier and a ton of other features. Here is a Youtube video showing you how to do that.
Ensure that you have Go installed. Download the necessary dependencies using the following command:
go mod tidy
Before running the API, Make sure to make all the migrations to the database by running the following command:
go run migrate/migrate.go
To start the application locally, run the following command,
Once the application is running, you can access the API at http://localhost:8080
go run cmd/main.go
To run the application using docker container, first create an image using following command:
docker build -t image-name .
After creating an image, spin up docker a container using this image, make sure to pass in the Environment variables:
docker run --name container-name -p 8080:8080 -e PORT=8080 -e DB_URL="postgres://<username>:<password>@<IpAddress>:<PORT>" image-name
If you have docker and docker-compose installed you don't need to do much inorder to run the application, just run the following command and it will start the application.
docker-compose up --build
You can use tools like Postman or cURL to test the API endpoints. Below is an example using "cURL":
curl -X GET http://localhost:8080/todos
If you have any questions or feedback, feel free to reach out: