Go(Golang) with Fiber and PostgreSQL
Mar 14 2024

Go(Golang) with Fiber and PostgreSQL


go mod init github.com/your/repo

Quickstart

A simple of main.go file.

package main

import (
	"fmt"

	"github.com/gofiber/fiber/v2"
	"github.com/joho/godotenv"
	"github.com/soulinmaikadua/my-go-fiber/pkg/routes"
)


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

	// Load variables from .env file
	godotenv.Load()

	app.Get("/", func(c *fiber.Ctx) error {
		return c.JSON(fiber.Map{
			"message": "Hello world!",
		})
	})

	// Routes
	routes.AuthRoutes(app)
	routes.UserRoutes(app)
	routes.PostRoutes(app)
	routes.NotFoundRoute(app)

	port := 6000
	addr := fmt.Sprintf(":%d", port)
	fmt.Printf("Server is running on http://localhost:%d\n", port)

	err := app.Listen(addr)
	if err != nil {
		fmt.Printf("Error starting the server: %v\n", err)
	}
}

Github repository

Related projects