create a fiber website in go that prints the date in go

To create a fiber website in go that prints the date, you need to follow these steps:

  1. Install fiber package using the following command:
main.go
go get -u github.com/gofiber/fiber/v2
38 chars
2 lines
  1. Import the required packages:
main.go
package main

import (
    "fmt"
    "time"
    "github.com/gofiber/fiber/v2"
)
80 chars
8 lines
  1. Create a fiber application instance:
main.go
app := fiber.New()
19 chars
2 lines
  1. Define a function to handle the requests:
main.go
func getDate(c *fiber.Ctx) error {
    currentTime := time.Now()
    formattedTime := currentTime.Format("01-02-2006")
    return c.SendString("Today's date is: " + formattedTime)
}
182 chars
6 lines

This function uses the time.Now() function to get the current time and then formats it using the Format() function with the required layout.

  1. Define a route to call this function:
main.go
app.Get("/", getDate)
22 chars
2 lines

This creates a route that listens to GET requests to the root of the website and calls the getDate() function to handle the request.

  1. Start the fiber application:
main.go
app.Listen(":3000")
20 chars
2 lines

This starts the application and listens on port 3000 for incoming requests.

The complete code for this task would be as follows:

main.go
package main

import (
    "fmt"
    "time"
    "github.com/gofiber/fiber/v2"
)

func getDate(c *fiber.Ctx) error {
    currentTime := time.Now()
    formattedTime := currentTime.Format("01-02-2006")
    return c.SendString("Today's date is: " + formattedTime)
}

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

    app.Get("/", getDate)

    app.Listen(":3000")
}
355 chars
22 lines

You can now run this code using the go run command and visit http://localhost:3000/ to see the current date in the specified format.

gistlibby LogSnag