In this project, we develop A REST API with Go (or Golang) that performs CRUD (i.e., CREATE, READ, UPDATE AND DELETE) operations on jobs data saved on a PostgreSQL database.
Tech Stack: Go, PostgreSQL, Postman.
The project consists of these qualities:
- We work with structs and slices to create our model and store and pass data around.
- We create a router to register our API endpoints and handler functions.
- We make use of technologies such as the PostgreSQL database for persisting records, Postman for making API requests, third party packages such as gorilla/mux for routing, and other packages for loading environment variables, making http requests, encoding and decoding JSON, performing string conversions and checking types.
- We refactor our code and create a modular file structure.
To run the application on your local machine, set up your software environment first following the steps below.
(1) Install Go
(2) Install Go third party packages
- Get gorilla/mux
- Get gotenv
- Get pq
(3) Get Postman desktop agent
(4) Get ElephantSQL
- Set up a (free) database instance
- Create a new table in your instance called
jobs
with PostgreSQL syntax - Create a
.env
file in your project folder - Create a variable in your .env file called
ELEPHANTSQL_URL
and assign to it your database URL in quotes
To start and test the application:
Launch the application by running the following command in your terminal:
go build && ./jobs-list
You can access the API in two ways:
Open your web browser and navigate to:
http://localhost:8000/jobs
Alternatively, use Postman to send requests:
GET http://localhost:8000/jobs
Note: The application runs on port 8000 by default. Make sure this port is available on your system.
We recommend using Postman to make requests to the REST API. You can use either the desktop agent or browser version to send GET, POST, PUT, or DELETE requests.
To create a new job posting:
POST http://localhost:8000/jobs
Request body:
{
"title": "Software Engineer",
"company": "LLM AI",
"location": "San Francisco, CA",
"type": "Full-time"
}
The server will return a response containing the newly created job's ID (assumed to be 8 in subsequent examples).
To retrieve all jobs:
GET http://localhost:8000/jobs
To retrieve a specific job by ID:
GET http://localhost:8000/jobs/1
To update an existing job:
PUT http://localhost:8000/jobs
Request body:
{
"id": 8,
"title": "Machine Learning Engineer",
"company": "LLM AI",
"location": "New York, NY",
"type": "Full-time"
}
To delete a job by ID:
DELETE http://localhost:8000/jobs/8
For more detailed examples of using our API with Postman, check out our demo.