DEV Community

Cover image for Initial Setup 🛠
Јане Џумеркоски
Јане Џумеркоски

Posted on

Initial Setup 🛠

We will need Go 1.14+ installed in our system. You can check the Go version in your terminal by writing go version. If you still don't have Go on your system, you can easily download one with brew install go using Homebrew. Or follow the official documentation on how to download and install Go.

Let’s initialize the project.

go mod init go-redis-url-shortener
Enter fullscreen mode Exit fullscreen mode

📝 This will create go.mod file in the project folder.

After successfully initializing our project, the next step is creating the main.go file and add the following code.

package main

import "fmt"

func main() {
    fmt.Printf("Welcome to Go URL Shortener with Redis !🚀")
}  
Enter fullscreen mode Exit fullscreen mode

If we run go run main.go in the terminal inside the project directory (where our main.go file is located) we should see this output:

Welcome to Go URL Shortener with Redis !🚀

Amazing! The project setup was successful.

Now let’s add Echo, an open-source Go web application framework, so we can easily build our web application. For more details visit their website.

go get github.com/labstack/echo/v4
Enter fullscreen mode Exit fullscreen mode

📝 This will create go.sum file in the project folder.

All right, all right. Now we are ready to start the web server and return some data in JSON format. Update the main.go file to reflect these changes.

package main

import (
    "github.com/labstack/echo/v4"
    "net/http"
)

func main() {
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        return c.JSON(http.StatusOK, map[string]interface{}{
            "message": "Welcome to Go URL Shortener with Redis !🚀",
        })
    })
    e.Logger.Fatal(e.Start(":1323"))
}

Enter fullscreen mode Exit fullscreen mode

Run again the main.go file (command: go run main.go ) and open http://localhost:1323/ in your browser or other rest client tool. The output should look like this.

{
  "message": "Welcome to Go URL Shortener with Redis !🚀"
}
Enter fullscreen mode Exit fullscreen mode

If you got this JSON response, it’s time to celebrate. This means that our setup was done successfully. 👏


Originally published at projectex.dev

Top comments (0)