This project is an example of implementing CRUD (Create, Read, Update, Delete) operations in Go language to work with data in PostgreSQL database.
main.go
: Server main filemodels/task.go
: Defining a model for the databasemigrate/postModel.go
/: Database migrationinitializers/database.go
/: Connecting to the databaseinitializers/loadEnvVariables.go
/: Environment variablescontrollers/postsController.go
/: Realization of CRUD queries
- Creating new records
- Updating existing records
- Reading data from the database
- Deleting records from the database
-
Make sure you have Go installed. If not, you can download it from official website
-
Clone the repository to your computer:
git clone https://github.com/rovezuka/go-crud
- Install the required dependencies:
Go to the root directory of the project and run go mod tidy
to install all required dependencies
- Run the application:
Execute the command go run main.go
to run the project on the local server. By default, the project will be available at `http://localhost:8080``
-
Navigate to the main page of the application
-
Add a new task by entering its description and clicking the "Add" button
-
Mark tasks as completed by pressing the corresponding button
-
Delete tasks by clicking on the "Delete" button
The project provides the following routes for working with data:
POST /posts
: Create a new record. Send a JSON request with the record dataPUT /posts/:id
: Update an existing record by id. Send a JSON request with the updated record dataGET /posts
: Get a list of all recordsGET /posts/:id
: Get record information by idDELETE /posts/:id
: Delete a record by identifier
For each request, the API expects a JSON object with data in the request body and returns a JSON response with the result of the operation.
Example of a request to create a new record using Postman:
http://localhost:8080/posts
{
"title": "Record title",
"content": "Content of the record"
}
Example of a response in Postman:
{
"id": 1,
"title": "Record title",
"content": "Content of the record"
}
The project uses PostgreSQL database. To customize the database connection, specify the appropriate parameters in the .env file. Example .env file:
PORT=5432
DB_URL="YOUR_URL"
This project is distributed under the MIT license. See the LICENSE
file for details.