DEV Community

kartracer73
kartracer73

Posted on

Go and Fiber GET always get invoked first?

Hello. I am a newbie when it comes to GO and web development so let me start by apologizing for asking stupid questions.

I am trying to teach myself GO using the Fiber framework along with PostgreSQL. I came across a post that showed me a simple example of using GO with PostgreSQL. Here's post that I found https://blog.logrocket.com/building-simple-app-go-postgresql/ . I am trying to follow the steps outlined in the post and working through it step by step but came across a situation where I'm a bit puzzled. It would appear that "app.Get" in the post always gets invoked first. It does not matter if the app.Post is written before app.Get, the handler attached to the app.Get is always invoked first. I was wondering why that is? Is there something in the code that is causing that to happen? I'm sure there is a very simple explanation for this but I just can't see it. Again, sorry for the stupid questions.

Thank you in advance for your help.

Here's the code I've have taken from the post. When I run this, the output always displays the handler attached to the app.Get.

package main

import (
"fmt"
"log"
"os"

"github.com/gofiber/fiber/v2"
Enter fullscreen mode Exit fullscreen mode

)

func indexHandler(c *fiber.Ctx) error {
return c.SendString("Hello Index")
}

func postHandler(c *fiber.Ctx) error {
return c.SendString("Hello Post")
}

func putHandler(c *fiber.Ctx) error {
return c.SendString("Hello Put")
}

func deleteHandler(c *fiber.Ctx) error {
return c.SendString("Hello Delete")
}

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

app.Get("/", indexHandler) // Add this

app.Post("/", postHandler) // Add this

app.Put("/update", putHandler) // Add this

app.Delete("/delete", deleteHandler) // Add this

port := os.Getenv("PORT")
if port == "" {
    port = "3030"
}
log.Fatalln(app.Listen(fmt.Sprintf(":%v", port)))
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)