Unlock the Power of PostgreSQL with Go: Building a To-Do App

Why PostgreSQL?
PostgreSQL is a powerful, open-source object-relational database system with over thirty years of active development, earning it a strong reputation for reliability, feature robustness, and performance. In this article, we’ll explore how to harness its power in a Go application.

Getting Started
Before we dive into the project, make sure you have the following prerequisites set up:

  • Go: Install Go in your local environment as our programming language of choice.
  • PostgreSQL: Install PostgreSQL for development purposes, and consider a cloud offering like AWS Aurora for production.
  • pgAdmin 4: Download pgAdmin to visually manage your Postgres database.

Our Project: A Simple To-Do App
We’ll build a full-stack web application that performs CRUD operations on our Postgres database. Our to-do app will allow users to get, add, edit, and delete to-do items.

Setting Up the Server
Create a file named server.go in your project folder and add the following code:
“`go
package main

import (
“log”
“os”

"github.com/gofiber/fiber/v2"

)

func main() {
app := fiber.New()

//...

}

We'll create a new Fiber object and assign it to the
appvariable. Then, we'll check our environment variables for aPORT` variable and assign it to 3000 if it doesn’t exist.

Adding Routes
Let’s add four methods to handle GET, POST, PUT, and DELETE operations for our app:
go
app.Get("/", getIndex)
app.Post("/add", postHandler)
app.Put("/edit", putHandler)
app.Delete("/delete", deleteHandler)

Defining Handlers
We’ll define these methods to handle database interactions:
“`go
func getIndex(c *fiber.Ctx) error {
//…
}

func postHandler(c *fiber.Ctx) error {
//…
}

func putHandler(c *fiber.Ctx) error {
//…
}

func deleteHandler(c *fiber.Ctx) error {
//…
}

**Connecting to the Database**
Let's create a connection to our database:
go
import (
“database/sql”
_ “github.com/lib/pq”
)

func main() {
//…
db, err := sql.Open(“postgres”, “user:password@localhost/database”)
if err!= nil {
log.Fatal(err)
}
defer db.Close()
}

**Fleshing Out Handlers**
We'll update our handlers to accept a pointer to our database connection:
go
func getIndex(c *fiber.Ctx, db *sql.DB) error {
//…
}

func postHandler(c *fiber.Ctx, db *sql.DB) error {
//…
}

func putHandler(c *fiber.Ctx, db *sql.DB) error {
//…
}

func deleteHandler(c *fiber.Ctx, db *sql.DB) error {
//…
}

**Configuring Views**
We'll configure our Fiber app to serve HTML views:
go
app.Settings.Views.Layout = “./views”

Create a folder called `views` and add an `index.html` file:
html



To-Do App

To-Do App

    {{ range.todos }}

  • {{. }}
  • {{ end }}




**Adding Styles**
Create a `style.css` file in the `public` folder:
css
body {
background-color: #f2f2f2;
}

ul {
list-style: none;
padding: 0;
margin: 0;
}

li {
padding: 10px;
border-bottom: 1px solid #ccc;
}

**Starting the App**
Run the app with
go run main.goand visithttp://localhost:3000` to see the to-do app in action!

This tutorial demonstrates how to connect to a PostgreSQL database with Go and build a simple to-do application. There are many ways to improve this app, and we can’t wait to see what you build next!

Leave a Reply

Your email address will not be published. Required fields are marked *